【问题标题】:C# WPF custom control not responding to XAML properties at design-time?C# WPF 自定义控件在设计时不响应 XAML 属性?
【发布时间】:2010-10-15 09:45:47
【问题描述】:

我创建了一个 UserControl,它本质上是一个按钮。它上面有一个图像和一个标签,我创建了两个属性来设置图像的源和标签的文本,如下所示:

        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));

但是,当我将控件添加到我的页面时,控件不会响应我在 XAML 中设置的任何属性,例如<controls:MusicButton Icon="/SuCo;component/Resources/settings.png/> 什么都不做。

我做错了什么?

【问题讨论】:

    标签: c# wpf user-controls properties dependencies


    【解决方案1】:

    包装依赖属性的 CLR 属性应该从不除了调用 GetValueSetValue 之外有任何逻辑。那是因为他们甚至可能不会被调用。例如,XAML 编译器将通过直接调用 GetValue/SetValue 而不是使用您的 CLR 属性进行优化。

    如果需要在依赖属性更改时执行一些逻辑,请使用元数据:

    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
    }
    

    编辑

    在我的第一个答案中,我假设您的控件的 XAML(无论是来自模板还是直接在 UserControl 中)已正确连接到属性。您没有向我们展示 XAML,因此这可能是一个不正确的假设。我希望看到类似的东西:

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

    而且 - 重要的是 - 您的 DataContext 必须设置为控件本身。您可以通过各种不同的方式来执行此操作,但这里有一个从后面的代码设置它的非常简单的示例:

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

    【讨论】:

    • 我添加了 'dependencyObject.SetValue(Image.SourceProperty, e.NewValue);'进入 OnIconChanged void ,它似乎仍然什么都不做,无论是在设计时还是运行时。即使清除 XAML 中的默认 Source 值也无法修复它。还有什么建议吗? :P
    • 我的编辑有帮助吗?如果没有,你能发布你所有的代码和 XAML 吗?
    【解决方案2】:

    您是否也尝试过设置 text 属性?图像的来源可能是错误的。文字更直接。

    另外,在您的示例中,您错过了引号。因此,如果它是从您的真实代码中复制而来的,您可能需要检查一下。

    除了那些公认不太可能导致您的问题的次要原因,我建议在代码中设置属性以检查这是否有任何影响。如果有,那么你真的应该检查你的 XAML。

    由于您尚未发布其余代码,因此我无法确定您是否在其他地方存在可能影响控件的问题。

    是的,我知道我不是很有帮助,但我使用 WPF 只工作了一段时间。无论如何,希望它有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 2013-06-26
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多