我正在开发一个具有列表
的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");
}
}
选择项目必然会
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);
}
但没有运气......有什么建议吗?
编辑:
这是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>
即使我删除了样式模板,也没有选择第一项:/
答案 0 :(得分:2)
我找到了答案......感谢@Haris Hasan
有Visual stete
名为SelectedUnfocused
。刚从Selected
复制了属性,一切正常
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<DoubleAnimation Duration="0"
To=".75"
Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="fillColor2" />
<ColorAnimation Duration="0"
To="#FF8DC5F9"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
Storyboard.TargetName="fillColor2"
d:IsOptimized="True" />
<ColorAnimation Duration="0"
To="#FFBCC0C0"
Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
Storyboard.TargetName="fillColor"
d:IsOptimized="True" />
<DoubleAnimation Duration="0"
To="1"
Storyboard.TargetProperty="(Rectangle.RadiusX)"
Storyboard.TargetName="fillColor2"
d:IsOptimized="True" />
</Storyboard>
</VisualState>