Xaml Silverlight从绑定列表获取ListBoxItem
我在一个名为Downloads.XAML的文件中有一个列表框。绑定到该列表框的项目来自我的ViewModel中的绑定列表。我在不同的XAML控件中定义了ListBoxItem样式中的状态,需要根据绑定项上的属性集激发这些状态。我的问题是无法从ListBox获取ListBoxItem,因为.SelectedItem引用的是实际绑定的对象,而不是ListBox。我曾尝试使用ListBox.ItemContainerGenerator,但这只会在80%的时间返回ListItem,其余20%的时间返回null,因此不会在相关ListBoxItem上设置VisualState 我还尝试过通过XAML中的datatriggers设置状态,但同样,这在100%的情况下都不起作用。有人知道如何从绑定对象轻松获取ListBoxItem,以便在其上触发所需的VisualState吗 绑定到列表框中列表的项目是从另一个页面添加的-下载页面此时不可见,我想这就是为什么没有找到列表框项目或者没有触发状态的原因 以下是样式中ListBoxItem控件模板的XAML:Xaml Silverlight从绑定列表获取ListBoxItem,xaml,silverlight-4.0,mvvm-light,Xaml,Silverlight 4.0,Mvvm Light,我在一个名为Downloads.XAML的文件中有一个列表框。绑定到该列表框的项目来自我的ViewModel中的绑定列表。我在不同的XAML控件中定义了ListBoxItem样式中的状态,需要根据绑定项上的属性集激发这些状态。我的问题是无法从ListBox获取ListBoxItem,因为.SelectedItem引用的是实际绑定的对象,而不是ListBox。我曾尝试使用ListBox.ItemContainerGenerator,但这只会在80%的时间返回ListItem,其余20%的时间返回n
<Style x:Key="PrimaryDownloadsListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Padding" Value="3"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem" >
<Grid Margin="0" Height="71">
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="5"/>
<RowDefinition Height="52"/>
<RowDefinition Height="5"/>
<RowDefinition Height="5"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="184"/>
<ColumnDefinition Width="116"/>
<ColumnDefinition Width="220"/>
<ColumnDefinition Width="67"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimation Duration="0" To="#FF191919" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="fillColor2" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
<VisualStateGroup x:Name="LayoutStates">
<VisualState x:Name="AfterLoaded"/>
<VisualState x:Name="BeforeLoaded"/>
<VisualState x:Name="BeforeUnloaded"/>
</VisualStateGroup>
<VisualStateGroup x:Name="DownloadStates">
<VisualState x:Name="DownloadInProgress">
...
我确实尝试将触发器添加到样式中的ContentTemplate中,但它没有100%的工作时间:
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding DownloadState}" Value="Downloading">
<ei:GoToStateAction StateName="Focused"/>
<ei:GoToStateAction StateName="DownloadInProgress"/>
</ei:DataTrigger>
<ei:DataTrigger Binding="{Binding DownloadState}" Value="DownloadComplete">
<ei:GoToStateAction StateName="Focused"/>
<ei:GoToStateAction StateName="DownloadComplete"/>
</ei:DataTrigger>
</i:Interaction.Triggers>
我通过将视图模型中的属性设置为ContentTemplate中网格的templatedParent,然后引用ListBoxItem,成功地获得了ListBoxItem:
"{Binding RelativeSource={RelativeSource TemplatedParent}}"
希望这会对某些人有所帮助:)我在视图模型中将属性设置为ContentTemplate中网格的templatedParent,然后引用ListBoxItem,从而获得了ListBoxItem:
"{Binding RelativeSource={RelativeSource TemplatedParent}}"
希望这有助于某人:)