XAML从ItemsControl模板绑定 [英] XAML Binding from ItemsControl Template

查看:142
本文介绍了XAML从ItemsControl模板绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewModel设置为DataContext的视图。那个ViewModel有一个布尔属性,称之为IsInEditMode。如何从DataTemplate中的UserControl绑定(标记为Binding IsInEditMode)到外部的ViewModel?

  ; ItemsControl ItemTemplate ={StaticResource HomeItemTemplate}> 
< ItemsControl.Resources>
< DataTemplate x:Key =HomeItemTemplate>
< utils:PersonTextBox Property ={Binding IsInEditMode}/>
< / DataTemplate>
< /ItemsControl.Resources>
< / ItemsControl>


解决方案

您可以使用 RelativeSource 绑定到外部ItemsControl,然后绑定到其数据上下文:

 < utils :PersonTextBox Property ={Binding 
RelativeSource = {RelativeSource AncestorType = ItemsControl},
Path = DataContext.IsInEditMode}/>


I have a view that has a ViewModel set as DataContext. That ViewModel has a boolean Property, call it "IsInEditMode". How do I bind from the UserControl in the DataTemplate (marked as "Binding IsInEditMode") to the ViewModel on the outside?

<ItemsControl ItemTemplate="{StaticResource HomeItemTemplate}">
    <ItemsControl.Resources>
        <DataTemplate x:Key="HomeItemTemplate">
            <utils:PersonTextBox Property="{Binding IsInEditMode}"/>
        </DataTemplate>
    </ItemsControl.Resources>
</ItemsControl>

解决方案

You can use a RelativeSource binding to get to the outer ItemsControl, and then bind to its data context:

<utils:PersonTextBox Property="{Binding 
    RelativeSource={RelativeSource AncestorType=ItemsControl},
    Path=DataContext.IsInEditMode}"/>

这篇关于XAML从ItemsControl模板绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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