1

我有一个非常简单的基于按钮的控件,它显示一个椭圆,其颜色取自名为“Brush”的自定义依赖控件。

模板以正确的颜色显示椭圆,但触发器中的设置器无法识别“画笔”属性(错误在下面的 XAML 文件中突出显示)。

如何访问设置器中的“画笔”属性,以便我可以在 MouseOver 上更改其值?

XAML:

<Button x:Class="WpfTest.EllipseButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfTest"
        Style="{DynamicResource localStyle}"
        Name="ellipseButton">

  <Button.Resources>
    <Style x:Key="localStyle"
           TargetType="local:EllipseButton">
      <Style.Setters>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate>
              <Grid>
                <Ellipse Fill="{Binding ElementName=ellipseButton, Path=Brush}" />
              </Grid>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style.Setters>
      <Style.Triggers>
        <Trigger Property="IsMouseOver"
                 Value="True">

          <!-- ERROR HERE: The property "Brush" is not a dependency property. -->
          <Setter Property="Brush"
                  Value="Blue" />
          <!-- ERROR HERE: The "BrushProperty" is not recognized or is not accessible.  -->
          <Setter Property="BrushPropety"
                  Value="Blue" />

        </Trigger>
      </Style.Triggers>
    </Style>
  </Button.Resources>
  <Grid>
  </Grid>
</Button>

代码隐藏:

public partial class EllipseButton : Button
{
    public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
        "Fill",
        typeof(Brush),
        typeof(EllipseButton),
        new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray)));

    public Brush Brush
    {
        get
        {
            return (Brush)GetValue(BrushProperty);
        }
        set
        {
            SetValue(BrushProperty, value);
        }
    }

    public EllipseButton()
    {
        InitializeComponent();
    }
}
4

2 回答 2

1

您的属性称为“填充”而不是“刷”:

public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
        "Fill", //Error is here
        typeof(Brush),
        typeof(EllipseButton),
        new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray)));

将其更改为:

public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
        "Brush", 
        typeof(Brush),
        typeof(EllipseButton),
        new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray)));
于 2013-04-20T16:15:20.600 回答
1

好的,需要做几件事:

1) 将依赖属性名称重命名为“Brush”(它被错误地命名为“Fill”) - 感谢 HighCore

2)当控件在代码中使用时,删除设置“画笔”属性 - 本地值覆盖样式中的设置器。

3) 将样式从自定义控件移到更高级别(例如在“Themes\Generic.xaml”下)

4)从样式中删除 x:Key 属性并只保留类型名称(仍然不知道为什么......)

5)将“Brush”属性的默认值添加到样式设置器(再次,不知道为什么......)

固定 EllipseButton.xaml:

<Button x:Class="WpfTest.EllipseButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid/>
</Button>

固定代码隐藏:

public partial class EllipseButton
{
    public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
        "Brush",
        typeof(Brush),
        typeof(EllipseButton),
        new FrameworkPropertyMetadata(null));

    public Brush Brush
    {
        get
        {
            return (Brush)GetValue(BrushProperty);
        }
        set
        {
            SetValue(BrushProperty, value);
        }
    }

    public EllipseButton()
    {
        InitializeComponent();
    }
}

固定样式(Generic.xaml):

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfTest">

    <Style TargetType="local:EllipseButton">
        <Style.Setters>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Grid>
                            <Ellipse Fill="{Binding ElementName=ellipseButton, Path=Brush}"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Brush" Value="Pink"/>
        </Style.Setters>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Brush" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>
于 2013-04-20T23:27:07.240 回答