我已经以ToggleButton
方式插入Grid
这样的控件:
xmlns:LSControls="clr-namespace:LS"
然后:
<LSControls:Favourite />
我需要做的是绑定IsSelected
的每个项目的ListView
属性,事实上我已将控件插入ListView中,如下所示:
<ListView x:Name="AllMatches" ItemsSource="{Binding Source={StaticResource GroupedItems}}" SelectionChanged="AllMatches_SelectionChanged">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<LSControls:Favourite />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
这将显示我ListView
的eac项目中的控件,但我需要根据ToggleButton
的值检查或取消选中IsSelected
,此属性适用于每个项目AllMatches
来源。
怎么做呢?
控件的结构如下:
<UserControl.Resources>
<BitmapImage x:Key="Star" UriSource="add-favourite.png" />
<BitmapImage x:Key="StarEmpty" UriSource="add-favourite-empty.png" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
<ToggleButton Focusable="False" Width="19" Height="19">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
RecognizesAccessKey="True"
HorizontalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding = "{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="False" />
<Condition Binding = "{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsEnabled}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter Property="Content">
<Setter.Value>
<Image Source="{StaticResource StarEmpty}"/>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="Add to favourite."/>
</MultiDataTrigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content">
<Setter.Value>
<Image Source="{StaticResource Star}"/>
</Setter.Value>
</Setter>
<Setter Property="ToolTip" Value="Remove from Favourite."/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Grid>