简体   繁体   English

C#WPF自定义控件在设计时不响应XAML属性?

[英]C# WPF custom control not responding to XAML properties at design-time?

I've created a UserControl which is essentially a button. 我创建了一个UserControl,它实际上是一个按钮。 It's got an Image and a Label on it and I've created two properties to set the Image's source and the Label's text like so: 它上面有一个图像和一个标签,我创建了两个属性来设置图像的源和标签的文本,如下所示:

        public ImageSource Icon
    {
        get { return (ImageSource)this.GetValue(IconProperty); }
        set { this.SetValue(IconProperty, value); icon.Source = value; }
    }
    public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(NavigationButton));

    public string Text
    {
        get { return (string)this.GetValue(TextProperty); }
        set { this.SetValue(TextProperty, value); label.Content = value; }
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(NavigationButton));

However, when I've added the control to my Page, the controls wont respond to any properties I set in XAML, eg <controls:MusicButton Icon="/SuCo;component/Resources/settings.png/> does nothing. 但是,当我将控件添加到Page中时,控件将不会响应我在XAML中设置的任何属性,例如<controls:MusicButton Icon="/SuCo;component/Resources/settings.png/>不会执行任何操作。

What am I doing wrong? 我究竟做错了什么?

CLR properties that wrap dependency properties should never have any logic other than calling GetValue and SetValue . CLR性能那套依赖属性应该比调用其他任何逻辑GetValueSetValue That is because they may not even be called. 那是因为它们甚至可能不会被调用。 For example, the XAML compiler will optimize by calling GetValue / SetValue directly rather than using your CLR property. 例如,XAML编译器将通过直接调用GetValue / SetValue而不是使用CLR属性来进行优化。

If you need to execute some logic when a dependency property is changed, use metadata: 如果在更改依赖项属性时需要执行一些逻辑,请使用元数据:

public ImageSource Icon
{
    get { return (ImageSource)this.GetValue(IconProperty); }
    set { this.SetValue(IconProperty, value); }
}

public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(ImageSource), typeof(NavigationButton), new FrameworkPropertyMetadata(OnIconChanged));

private static void OnIconChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
    //do whatever you want here - the first parameter is your DependencyObject
}

EDIT 编辑

In my first answer, I assumed your control's XAML (be it from a template or directly in a UserControl) is correctly hooked up to the properties. 在我的第一个答案中,我假设您控件的XAML(无论是来自模板还是直接位于UserControl中)已正确连接到属性。 You haven't showed us that XAML, so it was perhaps an incorrect assumption. 您尚未向我们展示XAML,因此这可能是一个错误的假设。 I'd expect to see something like: 我希望看到类似的东西:

<StackPanel>
    <Image Source="{Binding Icon}"/>
    <TextBlock Text="{Binding Text}"/>
</StackPanel>

And - importantly - your DataContext must be set to the control itself. 而且-重要的是-您的DataContext必须设置为控件本身。 You can do this in various different ways, but here is a very simple example of setting it from the code behind: 您可以通过各种不同的方式来执行此操作,但是这里有一个非常简单的示例,可以通过后面的代码进行设置:

public YourControl()
{
    InitializeComponent();
    //bindings without an explicit source will look at their DataContext, which is this control
    DataContext = this;
}

Have you tried setting the text property as well? 您是否也尝试过设置text属性? The source of an image may just be wrong. 图像来源可能只是错误的。 Text is much more straight forward. 文字更直接。

Also, in your example, you missed a quotation mark. 另外,在您的示例中,您错过了引号。 So if it's copied from your real code, you may want to check that. 因此,如果它是从您的真实代码中复制的,则可能需要检查一下。

Barring those minor admittedly unlikely causes for your problem, I'd suggest setting the properties in code to check whether that has any effect. 除了那些不太可能引起您的问题的小原因,我建议在代码中设置属性以检查是否有任何作用。 If it has, then you should really check your XAML. 如果有,那么您应该真正检查XAML。

Since you haven't posted the rest of your code, I can't really tell if you have problems somewhere else that might affect the control. 由于您尚未发布其余代码,因此我无法真正判断您是否在其他地方可能会影响控件。

And yes, I know I'm not very helpful, but I've been working with WPF for only a little while. 是的,我知道我不是很有帮助,但是我与WPF的合作只有一段时间了。 Hope it helps anyway. 希望它能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM