未使用DataTemplate DataType绑定属性

编程入门 行业动态 更新时间:2024-10-21 15:38:36
本文介绍了未使用DataTemplate DataType绑定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个主要的视图模型,可以创建许多子视图模型

I have a main viewmodel which create many child viewmodel

通过以下方式定义子vm数据模板:

The child vm datatemplate is definied in the following way:

<DataTemplate DataType="{x:Type vm:ChildVM}"> <view:childview /> </DataTemplate>

子视图具有以下代码:

<TextBox> <i:Interaction.Behaviors> <behavior:AppendTextBehavior AppendTextAction="{Binding AppendTextAction, Mode=OneWayToSource}" /> </i:Interaction.Behaviors> </TextBox>

行为类:

public class AppendTextBehavior : Behavior<TextBox> { public Action<string> AppendTextAction { get { return (Action<string>)GetValue(AppendTextActionProperty); } set { SetValue(AppendTextActionProperty, value); } } public static readonly DependencyProperty AppendTextActionProperty = DependencyProperty.Register("AppendTextAction", typeof(Action<string>), typeof(AppendTextBehavior)); protected override void OnAttached() { base.OnAttached(); SetCurrentValue(AppendTextActionProperty, (Action<string>)AssociatedObject.AppendText); AssociatedObject.TextChanged += AssociatedObject_TextChanged; } void AssociatedObject_TextChanged(object sender, EventArgs e) { // does something } }

ChildVM:

ChildVM :

private Action<string> appendTextAction; public Action<string> AppendTextAction { get { return appendTextAction; } set { appendTextAction = value; RaisePropertyChanged(() => AppendTextAction); } }

推荐答案

F加星标,

Hi F Starred,

>>显然,上面的代码是错误的,因为我不需要再次创建子视图模型.我该怎么做才能使AppendTextAction绑定起作用?

根据您的描述,我认为这可能是您的OnAttached()事件中的一些错误.您可以尝试更改代码.

From your description, I think, it is maybe some errors in your OnAttached() event. You can try to change the code.

protected override void OnAttached() { //set AppendTextAction values AppendTextAction = (Action<string>)AssociatedObject.AppendText; AssociatedObject.TextChanged += (sender, a) => { if (!IsFocused) { AssociatedObject.Focus(); AssociatedObject.Select(AssociatedObject.Text.Length, 0); } }; base.OnAttached(); }

以下文章供您参考.希望对您有帮助.

The following article for your reference. I hope it will helpful for you.

WPF中的附加行为: www.mindscapehq/blog/index .php/2009/02/01/attached-behaviours-in-wpf/

Attached behaviours in WPF: www.mindscapehq/blog/index.php/2009/02/01/attached-behaviours-in-wpf/

注意:此响应包含对第三方万维网站点的引用. Microsoft为方便您而提供此信息. Microsoft不控制这些站点,也没有测试在这些站点上找到的任何软件或信息;所以, Microsoft无法对在此找到的任何软件或信息的质量,安全性或适用性做出任何陈述.使用Internet上发现的任何软件都存在固有的危险,Microsoft提醒您确保自己 从Internet上检索任何软件之前,请完全了解风险.

Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

最好的问候,

吕汉楠

更多推荐

未使用DataTemplate DataType绑定属性

本文发布于:2023-11-07 05:32:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565674.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性   DataTemplate   DataType

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!