在Setter.Value标记中访问DependencyProperty的值

时间:2013-03-29 12:14:10

标签: wpf styles dependency-properties setter

我有一个控制器,当鼠标越过它时会改变它的颜色。我试图通过使用控件样式来解决它:

<Style TargetType="{x:Type local:Control}">
     <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="{x:Null}" />
    <Setter Property="MouseHoverBackgroundColor" Value="#FFFFEFBB" />
    <Setter Property="MouseHoverBorderColor" Value="#FFE5C365" />
    <Setter Property="MouseHoverBorderThickness" Value="2" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">

            <Setter Property="Background">
                <Setter.Value>
                    <!-- Here I have to get the Value of the current "MouseHoverBackgroundColor"-DependencyProperty. -->
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush">
        <Setter.Value>
            <!-- MouseHoverBorderColor -->
        </Setter.Value>
        </Setter>   
            <Setter Property="BorderThickness">
                <Setter.Value>
            <!-- MouseHoverBorderColor -->
            </Setter.Value>
            </Setter> 

        </Trigger>
    </Style.Triggers>
</Style>

我实际上不知道如何访问DependencyProperty的当前值。

此致

user1574054

1 个答案:

答案 0 :(得分:0)

我猜您不想为MouseHoverBorderColor设置BorderThickness,就像您的问题要求一样,而是使用MouseHoverBorderThickness

<Style TargetType="{x:Type local:Control}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="{x:Null}" />
    <Setter Property="MouseHoverBackgroundColor" Value="#FFFFEFBB" />
    <Setter Property="MouseHoverBorderColor" Value="#FFE5C365" />
    <Setter Property="MouseHoverBorderThickness" Value="2" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="true">    
            <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouseHoverBackgroundColor}">
            </Setter>
            <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouseHoverBorderColor}">
            </Setter>   
            <Setter Property="BorderThickness" Value="{Binding RelativeSource={RelativeSource Self}, Path=MouseHoverBorderThickness}">
            </Setter>    
        </Trigger>
    </Style.Triggers>
</Style>

我希望你有另一个原因可以使用这种方法,虽然有一个单独的DP用于悬停颜色和排序。

如果您只想更改触发器上的颜色,只需创建画笔作为样式资源,并在调用触发器时应用相应的画笔。保存您必须为这些东西创建依赖属性

用类似的东西说(你可以将ControlTemplate更改为你想要的东西):

<Style x:Key="ButtonStyle"
        TargetType="{x:Type Button}">
  <Style.Resources>
    <SolidColorBrush x:Key="Button.IsMouseOver.Background"
                      Color="#FFFFEFBB" />
    <SolidColorBrush x:Key="Button.IsMouseOver.BorderColor"
                      Color="#FFE5C365" />
    <Thickness x:Key="Button.IsMouseOver.Thickness">2</Thickness>
  </Style.Resources>
  <Setter Property="Background"
          Value="Transparent" />
  <Setter Property="BorderBrush"
          Value="{x:Null}" />
  <Setter Property="BorderThickness"
          Value="0" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
        <Border BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}">
          <ContentPresenter />
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Style.Triggers>
    <Trigger Property="IsMouseOver"
              Value="true">
      <Setter Property="Background"
              Value="{StaticResource Button.IsMouseOver.Background}" />
      <Setter Property="BorderBrush"
              Value="{StaticResource Button.IsMouseOver.BorderColor}" />
      <Setter Property="BorderThickness"
              Value="{StaticResource Button.IsMouseOver.Thickness}" />
    </Trigger>
  </Style.Triggers>
</Style>