如何绑定外部控件的属性?

时间:2016-09-15 12:33:22

标签: wpf xaml listview

我已经以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>

0 个答案:

没有答案