WPF:从具有自定义行为的类绑定到属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24479754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:01:46  来源:igfitidea点击:

WPF: binding to properties from class with custom behavior

c#wpfxamlbindingtriggers

提问by gbk

using WPF, c#, VS 2012

使用 WPF、C#、VS 2012

Try to implement some custom behaviors for UI with WPF. Currently create class that inherited from Behavior<FrameworkElement>

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

Idea: create one area on UI for entering name (I used textBox) - another area (Rectangle) - press and see some action with data from prev field.

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

What was done:

做了什么:

Class for implementing idea (class with Behavior)

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

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:

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>

Got error : A 'Binding' cannot be set on the 'Words' property of type 'SayHello'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject...

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

If i change part of XAML to

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

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

Question: are it's possible to create binding to properties for class with custom behavior? If yes, how can i do this?

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

EDIT:

编辑:

As was suggested by vfabre - changed property to dependency property

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

Result

结果

enter image description here

在此处输入图片说明

回答by vfabre

Maybe the error message give us the solution. You have to convert your Wordsproperty to a dependency propery the following is an example:

也许错误信息给了我们解决方案。您必须将您的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; }

I recommend you to use the code snnipet propdp. Tipping propdpand then tab+tab.

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

Hereyou can find more information about dependency properties.

您可以在此处找到有关依赖项属性的更多信息。

Regards

问候

EditChanged the default value from string.

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