C# WPF:使用自定义行为从类绑定到属性
使用WPF,c#,VS 2012 尝试使用WPF为UI实现一些自定义行为。 当前创建从C# WPF:使用自定义行为从类绑定到属性,c#,wpf,xaml,binding,triggers,C#,Wpf,Xaml,Binding,Triggers,使用WPF,c#,VS 2012 尝试使用WPF为UI实现一些自定义行为。 当前创建从行为继承的类 想法:在UI上创建一个区域用于输入姓名(我使用的是文本框)-另一个区域(矩形)-按下并查看来自prev字段的数据的一些操作 所做的工作: 实现理念的课程(有行为的课程) 所有作品(仅指移除装订) 问题:可以为具有自定义行为的类创建属性绑定吗?如果是,我怎么做 编辑: 正如vfabre所建议的,将属性更改为依赖属性 public string Words { get {
行为继承的类
想法:在UI上创建一个区域用于输入姓名(我使用的是文本框)-另一个区域(矩形)-按下并查看来自prev字段的数据的一些操作
所做的工作:
实现理念的课程(有行为的课程)
- 所有作品(仅指移除装订)
问题:可以为具有自定义行为的类创建属性绑定吗?如果是,我怎么做
编辑:
正如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)));
结果
也许错误消息为我们提供了解决方案。您必须将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; }
我建议您使用代码snnipetpropdp
。小费propdp
然后tab+tab
您可以找到有关依赖项属性的更多信息
问候
编辑
更改了字符串中的默认值。现在使用binding-ok试试,但是默认值-类型不匹配还有另一个问题,需要解决-。现在它的工作,请查看我的编辑
<Rectangle Stroke="Blue" Canvas.Top="250" Fill="Aquamarine"
Width="200" Height="50">
<i:Interaction.Behaviors>
<local:SayHello
Words="Adbracadabra"/>
</i:Interaction.Behaviors>
</Rectangle>
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)));
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; }