触发在ItemTemplate中设置属性

时间:2015-01-20 08:36:47

标签: c# wpf controltemplate

我有UserControl

的以下定义
<UserControl x:Class="Logger.View.AdvancedToggleButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:converter="clr-namespace:Logger.Converter"
             xmlns:commonControls="clr-namespace:Logger.CommonControls"
             mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <UserControl.Resources>
        <converter:CountToVisibilityConverter x:Key="CountToVisibilityConverter" />
    </UserControl.Resources>
    <ItemsControl ItemsSource="{Binding LogElements, UpdateSourceTrigger=PropertyChanged}">
        <ItemsControl.Resources>
            <Style TargetType="{x:Type ToggleButton}" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ToggleButton}">
                            <Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="container" 
                                    BorderThickness="1" Margin="1,2,1,1" Padding="2,2,4,2" BorderBrush="Black">
                                <ContentPresenter x:Name="contentPresenter" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsChecked" Value="True">
                                    <Setter Property="Background" TargetName="container" Value="{Binding SelectionBrush, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
                                    <Setter Property="BorderThickness" TargetName="container" Value="1,1,1,0"></Setter>
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Cursor" Value="Hand"/>
                                    <Setter Property="Background" TargetName="container" Value="{Binding SelectionBrush, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
                                    <Setter Property="Background" Value="{Binding SelectionBrush, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <ToggleButton Margin="2,1"
                        IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                        Command="{Binding DataContext.ToggleCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                        CommandParameter="{Binding}">
                        <ToggleButton.Content>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="4,0,5,0" x:Name="tbText"/>

                                <Border Grid.Column="1" Margin="0,-1,-3,-1" MinWidth="20"
                                        VerticalAlignment="Stretch" HorizontalAlignment="Center"
                                        Background="Yellow" BorderBrush="LightGray" BorderThickness="1,0,0,0"
                                        Visibility="{Binding WarningCount, Converter={StaticResource CountToVisibilityConverter}}"
                                        Cursor="Hand">
                                    <commonControls:ActionLink HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding WarningCount}" 
                                                       FontFamily="Consolas" Margin="2,0" Command="{Binding DataContext.NavigateToWarningCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
                                </Border>

                                <Border Grid.Column="2" Margin="3,-1,-3,-1" MinWidth="20"
                                        VerticalAlignment="Stretch" HorizontalAlignment="Center"
                                        Background="Red" BorderBrush="LightGray" BorderThickness="1,0,0,0"
                                        Visibility="{Binding ErrorCount, Converter={StaticResource CountToVisibilityConverter}}"
                                        Cursor="Hand" >
                                    <commonControls:ActionLink HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ErrorCount}" 
                                                       FontFamily="Consolas" Foreground="White" Margin="2,0" />
                                </Border>
                            </Grid>
                        </ToggleButton.Content>
                    </ToggleButton>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</UserControl>

一切都很好。现在,我已向此UserControl添加了另一个属性,以将已检查的BackgroundColor - ToggleButton(tbText)的TextBlock设置为给定的SolidColorBrush

我现在的问题是,我不知道如何正确设置触发器。

ControlTemplate.TriggersIsChecked时,我尝试将触发器添加到True。但我不知道如何从这一点访问tbText。这是触发器的正确​​位置还是我必须在较低级别添加另一个触发器?

1 个答案:

答案 0 :(得分:1)

您的ToggleButton.IsChecked属性是绑定到IsSelected属性的数据,因此默认情况下,当TextBlock.Background属性为IsSelected时,您还需要设置true 。你可以这样做:

<TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="4,0,5,0" 
    x:Name="tbText">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                    <Setter Property="Background" Value="{Binding YourColourBrush, 
                        RelativeSource={RelativeSource AncestorType={x:Type 
                        YourPrefix:YourUserControl}}}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>

或者,您可以直接将数据绑定到ToggleButton.IsChecked属性,如下所示:

<TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="4,0,5,0" 
    x:Name="tbText">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource
                    AncestorType={x:Type ToggleButton}}}" Value="True">
                    <Setter Property="Background" Value="{Binding YourColourBrush, 
                        RelativeSource={RelativeSource AncestorType={x:Type 
                        YourPrefix:YourUserControl}}}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>