Я занимаюсь разработкой приложения Silverlight со списком
<ListBox x:Name="_list_collection"
SelectionChanged="SelectionChanged"
ScrollViewer.VerticalScrollBarVisibility="Auto"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
ItemsSource="{Binding Collection, Mode=TwoWay}"
ItemTemplate="{StaticResource ScheduleListItemDataTemplate}"
ItemContainerStyle="{StaticResource ScheduleListItemContainerDataTemplate}" Margin="10" />
Источник предметов привязан к этому
public ObservableCollection<ScheduleDto> Collection
{
get { return _collection; }
set
{
_collection = value;
OnPropertyChanged("Collection");
}
}
И выбранный элемент привязан к
public ScheduleDto SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
Элемент выбирается, потому что я вижу некоторые подробности о нем в отдельном окне. Моя единственная проблема в том, что этот пункт не выделен (синий фон). Я попробовал это, добавив обработчик события selectiong, который выглядит так
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem selectedItem = (ListBoxItem)_list_collection.ItemContainerGenerator.ContainerFromIndex(_list_collection.SelectedIndex);
VisualStateManager.GoToState(selectedItem, "Selected", true);
}
Но не повезло ... Любой совет?
Edit:
Это ItemTemplate
<DataTemplate x:Key="ScheduleListItemDataTemplate">
<Grid VerticalAlignment="Stretch"
d:DesignHeight="100">
<Grid.Resources>
<Converters1:ScheduleStatusConverter x:Key="ScheduleStatusConverter"/>
<Converters1:DateToStringConverter x:Key="DateToStringConverter"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.ColumnSpan="3"
HorizontalAlignment="Stretch"
Text="{Binding Name}"
Foreground="{Binding Converter={StaticResource ScheduleStatusConverter}}"
FontFamily="{StaticResource LabelTextStyle}"
FontSize="19" TextTrimming="WordEllipsis" VerticalAlignment="Center"/>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=[lblCreatedBy], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
<TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap"
Text="{Binding CreatedBy}"
FontFamily="{StaticResource LabelTextStyle}"
FontSize="16"
VerticalAlignment="Center"
Grid.Column="2"
Margin="0"
Grid.Row="1"
HorizontalAlignment="Right">
<TextBlock.Foreground>
<SolidColorBrush Color="{StaticResource FactualTextStyle}" />
</TextBlock.Foreground>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBlock Text="{Binding Path=[lblCoachingViewGridCreatedDate], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
<TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap"
Text="{Binding CreatedAt,Converter={StaticResource DateToStringConverter}}"
FontSize="16"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
</StackPanel>
</Grid>
<Grid Grid.Column="2"
Margin="0"
Grid.Row="1" Visibility="{Binding}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=[lblUpdatedBy], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
<TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap"
Text="{Binding UpdatedBy}"
FontFamily="{StaticResource LabelTextStyle}"
FontSize="16"
VerticalAlignment="Center"
Grid.Column="2"
Margin="0"
Grid.Row="1"
HorizontalAlignment="Right">
<TextBlock.Foreground>
<SolidColorBrush Color="{StaticResource FactualTextStyle}" />
</TextBlock.Foreground>
</TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBlock Text="{Binding Path=[lblUpdatedDate], Source={StaticResource Translations}}" VerticalAlignment="Center"/>
<TextBlock Text=":" Margin="2,0,2,0" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap"
Text="{Binding UpdatedAt,Converter={StaticResource DateToStringConverter}}"
FontSize="16"
VerticalAlignment="Center"
HorizontalAlignment="Right"/>
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
Даже если я удалю шаблон стиля, первый элемент не будет выбран: /