WPF自定义控件属性设置器

时间:2013-04-20 14:46:30

标签: wpf xaml custom-controls

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

模板显示具有正确颜色的椭圆,但触发器中的Setters无法识别“Brush”属性(下面的XAML文件中突出显示的错误)。

如何访问setter中的“Brush”属性,以便我可以在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();
    }
}

2 个答案:

答案 0 :(得分: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)));

答案 1 :(得分: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>