未使用DataTemplate DataType绑定属性 [英] Property not binded using DataTemplate DataType

查看:74
本文介绍了未使用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中的附加行为:
http://www.mindscapehq.com/blog/index .php/2009/02/01/attached-behaviours-in-wpf/

Attached behaviours in WPF:
http://www.mindscapehq.com/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绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆