При загрузке данных из БД возникает исключение InvalidOperationException с сообщением: Элемент коллекции должен быть пустым перед использованием ItemsSource. Знаю, что для заполнения DataGrid можно использовать коллекции Items или ItemsSource, но одновременно их использовать нельзя: если задано значение для свойства ItemsSource и в коде C# добавляется элемент в Items, возникает исключение. |
Опубликован: 08.07.2011 | Уровень: для всех | Доступ: платный
Дополнительный материал 2:
Коды и XAML описания документов
Код главной страницы класса SilverlightAppPersonal.MainPage
<UserControl x:Class="SilverlightAppPersonal.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800" xmlns:controls="clr-namespace: System.Windows.Controls;assembly=System.Windows.Controls"> <Grid x:Name="LayoutRoot" Background="White" Margin="10" VerticalAlignment="Top" Height="590" Width="779" <Grid.ColumnDefinitions> <ColumnDefinition Width="384"/> <ColumnDefinition Width="166*" /> <ColumnDefinition Width="229*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition Height="249*" /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <TextBlock Name="PersonList" Margin="5,5,145,5" Text="Список сотрудников" Foreground="#FF000085" FontStyle="Italic" FontSize="12" Grid.ColumnSpan="2" /> <TextBlock Name="EmployeeDefinition" Margin="5" Text="Данные по сотруднику" Foreground="#FF000085" FontStyle="Italic" FontSize="12" Grid.Column="2" TextAlignment="Center" /> <StackPanel Name="EmployeeAttributeLable" Orientation="Vertical" Grid.Row="1" Grid.Column="1" Margin="9,7,6,0"> <TextBlock Name="TextBlockLastName" Text="Фамилия" Height="25" TextAlignment="Right" TextWrapping="NoWrap"></TextBlock> <TextBlock Name="TextBlockFirstName" Text="Имя" Margin="2" Height="25" TextAlignment="Right" VerticalAlignment="Center"></TextBlock> <TextBlock Name="TextBlockSecondtName" Text="Отчество" Margin="2" Height="25" TextAlignment="Right" VerticalAlignment="Center"></TextBlock> <TextBlock Name="TextBlockNetName" Text="Сетевое имя" Margin="2" Height="25" TextAlignment="Right"></TextBlock> <TextBlock Name="TextBlockJodTitle" Text="Должность" Margin="2" Height="25" TextAlignment="Right"></TextBlock> <TextBlock Name="TextBlockStatus" Text="Статус" Margin="2" Height="25" TextAlignment="Right"></TextBlock> <TextBlock Name="TextBlockAccess" Text="Уроветь доступа" Margin="2" Height="25" TextAlignment="Right"></TextBlock> <TextBlock Name="TextBlockFirstDate" Text="Дата назначения" Margin="2" Height="25" TextAlignment="Right"></TextBlock> <TextBlock Name="TextBlockLastDate" Text="Дата увольнения" Margin="2" Height="25" TextAlignment="Right"></TextBlock> <StackPanel Name="EmployeeAttributeValue" Orientation="Vertical" Grid.Row="1" Grid.Column="2" > <TextBox Name="LastName" Height="25" Margin="2" ></TextBox> <TextBox Name="FirstName" Height="25" Margin="2"></TextBox> <TextBox Name="SecondtName" Height="25" Margin="2"></TextBox> <TextBox Name="NetName" Height="25" Margin="2"></TextBox> <ComboBox Height="25" Name="comboBoxTitle" Margin="2" /> <ComboBox Name="comboBoxStatus" Height="25" Margin="2" /> <ComboBox Height="25" Name="comboBoxAccess" Margin="2" /> <controls:DatePicker Name="datePickerFirstDate" Margin="2"/> <controls:DatePicker Name="datePickerLastDate" Margin="2" /> </StackPanel> <StackPanel Orientation="Horizontal" Grid.Row="2" Margin="0,0,17,0"> <Button Name="ButtonSave" Content="Сохранить" Margin="5" Padding="5" Height="25" IsEnabled="False" /> <Button Name="ButtonUndo" Content="Отменить" Margin="5" Padding="5" Height="25" IsEnabled="False" /> <Button Name="ButtonNew" Content="Создать" Margin="5" Padding="5" Height="25" IsEnabled="False" /> <Button Content="Загрузить" Height="25" Name="ButtonOpen" Margin="5" Padding="10,5,10,5" IsEnabled="False" /> </StackPanel> <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="2"> <Button Name="ButtonEdit" Content="Редактировать" Margin="5" Padding="10,5,10,5" Height="25" IsEnabled="False" /> <Button Name="ButtonDelete" Content="Удалить" Padding="10,5,10,5" Margin="35,0,0,0" Height="25" /> </StackPanel> <ListBox Grid.Row="1" Name="listBoxEmployees" HorizontalAlignment="Center" Margin="5,2,15,9" Padding="3" Width="364" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=EmployeeSurname}" /> <TextBlock Text=" " /> <TextBlock Text="{Binding Path=EmployeeName}" /> <TextBlock Text=" " /> <TextBlock Text="{Binding Path=EmployeePatronymic}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </UserControl>
Файл ColorsAndBrushesRD.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--Цвет выделения внешней рамки элемента списка при наведении мыши--> <Color x:Key="PhotoSelectedBGColor">#FF768EE4</Color> <!--Цвет выделения внешней рамки элемента списка--> <Color x:Key="ItemSelectedBGColor">#FF91D0DA</Color> <!--Цвет фона кнопки--> <Color x:Key="ButtoColor">#FFBDDBE2</Color> <!--Цвет рамки кнопки--> <Color x:Key="ButtoBorder">#FF313491</Color> <!--Цвет рамки кнопки при наведении мыши--> <Color x:Key="ButtoBorderMouseOver">#FFFF7D00</Color> <!--Кисть заливки внутренней рамки пункта ListBox--> <SolidColorBrush x:Key="PhotoBorderSolidBrush" Color="#FF346797" /> <!--Кисть заливки фона пункта списка ListBox--> <SolidColorBrush x:Key="PhotoBGSolidBrush" Color="#FFFFFFFF" /> <!--Кисть заливки внешней рамки выделенного пункта списка ListBox--> <SolidColorBrush x:Key="PhotoSelectedBGSolidBrush" Color="{StaticResource PhotoSelectedBGColor}"/> <!--Кисть заливки внешней рамки невыделенного пункта списка ListBox--> <SolidColorBrush x:Key="ListBoxItemPhotoActiveBGSolidBrush" Color="#00FFFFFF"/> </ResourceDictionary>
XAML-разметка стиля ListBoxItemStyle
Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"> <Setter Property="Padding" Value="1"/> <Setter Property="VerticalAlignment" Value="Top"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="Border" Padding="5" Width="200" Height="126" CornerRadius="5" Background="{StaticResource ListBoxItemPhotoActiveBGSolidBrush}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="MouseOver"> <Storyboard> <ColorAnimation Duration="0:0:0" To="{StaticResource PhotoSelectedBGColor}" Storyboard.TargetProperty= "(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border" /> </Storyboard> </VisualState> </VisualStateGroup> <VisualStateGroup x:Name="SelectionStates"> <VisualState x:Name="Selected"> <Storyboard> <ColorAnimation Duration="0:0:0" To="{StaticResource ItemSelectedBGColor}" Storyboard.TargetProperty= (Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Border" /> </Storyboard> </VisualState> <VisualState x:Name="Unselected" /> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Border BorderBrush="{StaticResource PhotoSelectedBGSolidBrush}" BorderThickness="1" CornerRadius="5"> <ContentPresenter x:Name="contentPresenter" /> </Border> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
XAML-разметка стиля ListBoxPhotoStyle
<Style x:Key="ListBoxPhotoStyle" TargetType="ListBox"> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBox"> <Border> <ScrollViewer x:Name="ScrollViewer" Style="{StaticResource ScrollViewerPhotoStyle}"> <ItemsPresenter/> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
XAML-разметка стиля ScrollViewerPhotoStyle
<Style x:Key="ScrollViewerPhotoStyle" TargetType="ScrollViewer"> <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/> <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ScrollViewer"> <Grid> <ScrollContentPresenter x:Name="ScrollContentPresenter" Cursor="{TemplateBinding Cursor}" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.Column="1" Grid.Row="1"/> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <ScrollBar x:Name="VerticalScrollBar" Grid.Row="0" Grid.Column="1" IsTabStop="False" Maximum="{TemplateBinding ScrollableHeight}" Height="250" Minimum="0" Orientation="Vertical" VerticalAlignment="Center" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{TemplateBinding VerticalOffset}" ViewportSize="{TemplateBinding ViewportHeight}" Width="18" Margin="10" HorizontalAlignment="Center" Opacity="0.5"/> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>