【问题标题】:How do I make this trigger work?如何使此触发器起作用?
【发布时间】:2012-03-26 23:50:27
【问题描述】:

我创建了一个用于我的应用程序的用户控件。它由一个带有两个重复按钮的网格中的组合框组成。此控件用于将在配备触摸屏的笔记本电脑上运行的应用程序中。这些按钮用于选择组合框中的下一个或上一个选项。这是控件的 Xaml:

<UserControl x:Class="CarSystem.CustomControls.TouchComboBox"
             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:cs="clr-namespace:CarSystem.CustomControls"
             mc:Ignorable="d"
             Focusable="True"
             GotFocus="UserControl_GotFocus">

    <UserControl.Resources>
        <Style x:Key="FocusedStyle" TargetType="{x:Type ComboBox}">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="{DynamicResource FocusedBackground}" />
                    <Setter Property="Foreground" Value="{DynamicResource FocusedForeground}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid Background="{Binding Path=GridBackground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
          Width="{Binding Path=Width, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <ComboBox Background="{Binding Path=Background, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
                  BorderBrush="{Binding Path=BorderBrush, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
                  DisplayMemberPath="{Binding Path=DisplayMemberPath, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  FlowDirection="{Binding Path=FlowDirection, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  Focusable="True"
                  FontFamily="{Binding Path=FontFamily, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  FontSize="{Binding Path=FontSize, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  FontStretch="{Binding Path=FontStretch, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  FontStyle="{Binding Path=FontStyle, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  FontWeight="{Binding Path=FontWeight, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
                  Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
                  HorizontalAlignment="{Binding Path=HorizontalAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  HorizontalContentAlignment="{Binding Path=HorizontalContentAlignment, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  Grid.Column="0" 
                  ItemsSource="{Binding Path=ItemsSource, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  Name="ChoicesPicker" 
                  SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  SelectedValue="{Binding Path=SelectedValue, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  SelectedValuePath="{Binding Path=SelectedValuePath, Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}" 
                  SelectionChanged="ChoicesPicker_SelectionChanged"
                  Style="{StaticResource FocusedStyle}"
                  TabIndex="{Binding Path=TabIndex, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
                  VerticalAlignment="Center"/>
        <RepeatButton Background="{DynamicResource ButtonBackground}" 
                      Click="SelectPreviousButton_Click"
                      Focusable="False"
                      Foreground="{DynamicResource ButtonForeground}" 
                      Grid.Column="1" 
                      IsTabStop="False"
                      Name="SelectPreviousButton">
            <Image Source="/CustomControls;component/Resources/VolumeUp.png" />
        </RepeatButton>
        <RepeatButton Background="{DynamicResource ButtonBackground}" 
                      Click="SelectNextButton_Click"
                      Focusable="False"
                      Foreground="{DynamicResource ButtonForeground}" 
                      Grid.Column="2" 
                      IsTabStop="False"
                      Name="SelectNextButton">
            <Image Source="/CustomControls;component/Resources/VolumeDown.png" />
        </RepeatButton>
    </Grid>
</UserControl>

我希望在 ComboBox 获得焦点时更改背景和前景画笔。 UserControl 资源中名为“FocusedStyle”的样式应该为我执行此操作,但它不起作用。背景和前景颜色永远不会改变。

我的代码有什么问题?

托尼

【问题讨论】:

    标签: wpf triggers styles


    【解决方案1】:

    如果在实际控件中设置了该属性,则不能从样式中设置该属性,因为在控件中设置的任何属性都具有优先权。

    试试这个:

    <UserControl.Resources>
        <Style x:Key="FocusedStyle" TargetType="{x:Type ComboBox}">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="{DynamicResource FocusedBackground}" />
                    <Setter Property="Foreground" Value="{DynamicResource FocusedForeground}" />
                </Trigger>
                <Trigger Property="IsFocused" Value="False">
                    <Setter Property="Background" Value="{DynamicResource UnFocusedBackground}" />
                    <Setter Property="Foreground" Value="{DynamicResource UnFocusedForeground}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    

    然后你需要制作一个普通背景的DynamicResource。

    然后删除

        Background="{Binding Path=Background, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
    

        Foreground="{Binding Path=Foreground, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type cs:TouchComboBox}}}"
    

    来自组合框。

    这样触发器将能够设置属性的值,因为该属性不是由控件本身设置的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2018-10-17
      • 2017-04-22
      相关资源
      最近更新 更多