【问题标题】:WPF: binding to properties from class with custom behaviorWPF:从具有自定义行为的类绑定到属性
【发布时间】:2014-08-20 05:05:53
【问题描述】:

使用 WPF、c#、VS 2012

尝试使用 WPF 为 UI 实现一些自定义行为。 当前创建继承自Behavior<FrameworkElement>的类

想法:在 UI 上创建一个区域用于输入名称(我使用文本框)-另一个区域(矩形)-按下并查看上一个字段中的数据的一些操作。

做了什么:

实现想法的类(带有行为的类)

class SayHello: Behavior<FrameworkElement>
{
    public string Words { get; set; }
    protected override void OnAttached()
    {
        AssociatedObject.MouseLeftButtonUp += OnMouseClick;
    }
    private void OnMouseClick(object sender, 
                System.Windows.Input.MouseButtonEventArgs e)
    {
        MessageBox.Show(string.Format("hello , {0}", Words));
    }
    protected override void OnDetaching()
    {
        AssociatedObject.MouseLeftButtonUp -= OnMouseClick;
    }        
}

XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
    xmlns:local="clr-namespace:CH08.MovingCircle"
    x:Class="CH08.MovingCircle.MainWindow"
    Title="MainWindow" Height="350" Width="525">
<Canvas>
    <Border Canvas.Top="220" BorderBrush="Black" 
            BorderThickness="2">
        <TextBox x:Name="_enteredWords" Width="200"/>
    </Border>

    <Rectangle Stroke="Blue" Canvas.Top="250" Fill="Aquamarine"
               Width="200" Height="50">
        <i:Interaction.Behaviors>
            <local:SayHello 
                Words="{Binding Text, ElementName=_enteredWords}"/>
        </i:Interaction.Behaviors>
    </Rectangle>

</Canvas>

出现错误:A 'Binding' cannot be set on the 'Words' property of type 'SayHello'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject...

如果我将 XAML 的一部分更改为

        <Rectangle Stroke="Blue" Canvas.Top="250" Fill="Aquamarine"
               Width="200" Height="50">
        <i:Interaction.Behaviors>
            <local:SayHello 
                Words="Adbracadabra"/>
        </i:Interaction.Behaviors>
    </Rectangle>
  • 一切正常(意味着只需删除绑定)。

问题:是否可以为具有自定义行为的类创建属性绑定?如果是,我该怎么做?

编辑:

按照 vfabre 的建议 - 将属性更改为依赖属性

 public string Words
    {
        get { return (string)GetValue(WordsProperty); }
        set { SetValue(WordsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Words. 
    //This enables animation, styling, binding, etc...
    public static  DependencyProperty WordsProperty =
        DependencyProperty.Register("Words", typeof(string),
        typeof(SayHello), new PropertyMetadata(default(string)));

结果

【问题讨论】:

    标签: c# wpf xaml binding triggers


    【解决方案1】:

    也许错误信息给了我们解决方案。您必须将您的 Words 属性转换为依赖属性,以下是一个示例:

    public string Words
        {
            get { return (string)GetValue(WordsProperty); }
            set { SetValue(WordsProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for Words.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty WordsProperty =
            DependencyProperty.Register("Words", typeof(string), typeof(SayHello), new PropertyMetadata(default(string)));
    
    
        //public string Words { get; set; }
    

    我建议您使用代码 snnipet propdp。小费propdp 然后tab+tab

    Here你可以找到更多关于依赖属性的信息。

    问候

    编辑 更改了字符串的默认值。

    【讨论】:

    • 试试吧,现在使用绑定 - 好的,但有另一个默认值问题 - 类型不匹配,用于解决 - stackoverflow.com/q/20398751/2012219。现在它的工作,请看我的编辑
    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多