Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF自定义控件属性设置器_Wpf_Xaml_Custom Controls - Fatal编程技术网

WPF自定义控件属性设置器

WPF自定义控件属性设置器,wpf,xaml,custom-controls,Wpf,Xaml,Custom Controls,我有一个非常简单的基于按钮的控件,它显示一个椭圆,颜色取自名为“笔刷”的自定义依赖项控件 模板显示具有正确颜色的椭圆,但触发器中的设置程序无法识别“笔刷”属性(错误在下面的XAML文件中突出显示) 如何访问setter中的“Brush”属性,以便在MouseOver上更改其值 XAML: 您的属性称为“填充”而不是“笔刷”: 改为: public static readonly DependencyProperty BrushProperty = DependencyProperty.Regis

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

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

如何访问setter中的“Brush”属性,以便在MouseOver上更改其值

XAML:

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

改为:

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

好的,需要做几件事:

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

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

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

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

5) 将“笔刷”属性的默认值添加到样式设置器(同样,不确定原因…)

修复了EllipseButton.xaml:

<Button x:Class="WpfTest.EllipseButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Grid/>
</Button>
固定样式(Generic.xaml):



谢谢。我已经解决了这个问题,现在编译器抱怨:“EllipseButton”TargetType与元素“Button”的类型不匹配。但是,如果我将样式的TargetType从local:EllipseButton更改为Button,它将无法识别“Brush”属性…我已经解决了问题并发布了答案。所有的新手错误——阅读亚当·内森的《WPF4释放》一书并没有让我从所有这些麻烦中解脱出来:(…谷歌,因此仍然是唯一真正的帮手。
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)));
<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();
    }
}
<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>