1

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

<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 的当前值。

真挚地

用户1574054

4

1 回答 1

0

我猜你不想像你的问题要求的那样设置,而是MouseHoverBorderColor使用BorderThicknessMouseHoverBorderThickness

<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>
于 2013-03-29T12:19:09.197 回答