我在Windows应用商店应用项目中有以下ListBox
<ListBox x:Name="ContractIndicators" Grid.Row="2" ItemsSource="{Binding Indicators, ElementName=pageRoot}" SelectedItem="{Binding SelectedIndicator, ElementName=pageRoot, Mode=TwoWay}"
ItemContainerStyle="{StaticResource ItemStyle}" ItemTemplate="{StaticResource ItemTemplate}" />
使用这种风格
<Style x:Key="ItemStyle" TargetType="ListBoxItem">
<Setter Property="Height" Value="60" />
<Setter Property="Width" Value="60" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Background="{TemplateBinding Background}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates" >
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="0" To="{StaticResource AccentColor}" />
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="0" To="{StaticResource AccentColor}" />
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedPressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="0" To="{StaticResource AccentColor}" />
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedPointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="fillColor" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" BeginTime="00:00:00" Duration="0" To="{StaticResource AccentColor}" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="fillColor" IsHitTestVisible="False" Fill="Transparent" Margin="5"
Width="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}"
Height="{Binding Height, RelativeSource={RelativeSource TemplatedParent}}" />
<ContentPresenter Content="{Binding}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ListBox有一个bide to
private ObservableCollection<object> indicators = new ObservableCollection<object>();
这是集合包含的项目类型
public class Indicator
{
public string Image { get; set; }
public Indicator(string image)
{
Image = image;
}
}
我想要的是更改ListBox中所选项目内的图像字段 我怎么能这样做?
这就是我的ListBox显示的内容,我想要的是拥有绿色图标,具体取决于所选的项目 如果我点击第4项,该项目将传递给绿色 这将意味着我的指标对象中位于该索引的ListBox内的Image字段将被更新