【问题标题】:WPF Custom Control Property SetterWPF 自定义控件属性设置器
【发布时间】:2013-04-13 20:23:13
【问题描述】:

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

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

如何访问设置器中的“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();
    }
}

【问题讨论】:

    标签: wpf xaml custom-controls


    【解决方案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)));
    

    【讨论】:

    • 谢谢。我已经解决了这个问题,现在编译器抱怨说:'EllipseButton' TargetType 与元素'Button'的类型不匹配。但是,如果我将样式的 TargetType 从 local:EllipseButton 更改为 Button,它无法识别“Brush”属性...
    • 我已经解决了这些问题并发布了答案。所有新手错误 - 阅读 Adam Nathan 的“WPF 4 Unleashed”一书并没有让我摆脱所有这些麻烦 :( ... Google 和 SO 仍然是唯一真正的帮手。
    【解决方案2】:

    好的,需要做几件事:

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

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

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多