突出显示列表框中的选定项目
我正在开发一个 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" />
itemsource 绑定到此
public ObservableCollection<ScheduleDto> Collection
{
get { return _collection; }
set
{
_collection = value;
OnPropertyChanged("Collection");
}
}
并且 selecteditem 绑定到
public ScheduleDto SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
正在选择该项目,因为我可以看到有关它的一些详细信息填充在单独的视图中。我唯一的问题是该项目没有突出显示(蓝色背景)。我已经尝试过添加一个选择更改事件处理程序,如下所示
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem selectedItem = (ListBoxItem)_list_collection.ItemContainerGenerator.ContainerFromIndex(_list_collection.SelectedIndex);
VisualStateManager.GoToState(selectedItem, "Selected", true);
}
但没有运气......有什么建议吗?
编辑:
这是 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>
即使我删除样式模板,也不会选择第一个项目:/
I am developing a Silverlight application which has a list
<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" />
The itemsource is bound to this
public ObservableCollection<ScheduleDto> Collection
{
get { return _collection; }
set
{
_collection = value;
OnPropertyChanged("Collection");
}
}
And the selecteditem is bound to
public ScheduleDto SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
The item is being selected because I can see some details about it populate in a separate view. My only problem is that this item is not highlighted (blue background). I have tried it by adding a selectiong changed event handler which looks like this
private void SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBoxItem selectedItem = (ListBoxItem)_list_collection.ItemContainerGenerator.ContainerFromIndex(_list_collection.SelectedIndex);
VisualStateManager.GoToState(selectedItem, "Selected", true);
}
But no luck... Any advice?
Edit:
This is the 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>
Even if I remove the style template the first item isn't selected :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了答案...感谢@Haris Hasan
有一个名为
SelectedUnfocused
的Visual stete
。只需从Selected
复制属性,一切正常I found the answer... Thanks to @Haris Hasan
There is a
Visual stete
calledSelectedUnfocused
. Just copied the properties fromSelected
and everything works fine