绑定数据表到ContentControl的内容

绑定数据表到ContentControl的内容

本文介绍了绑定数据表到ContentControl的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文本框文本属性(在datatemplate内)绑定到ContentControl Content属性?

(不通过ElementName绑定)

How to bind textbox text property, within datatemplate, to ContentControl Content property?
(Without bindng via ElementName)

这是我的代码(这不工作):

This is my code( that doesn't work):

<Window.Resources>
    <DataTemplate x:Key="Temp">
       <TextBox TextWrapping="Wrap" Text="{TemplateBinding Content}" Height="20" Width="Auto"/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ContentControl  ContentTemplate="{DynamicResource Temp}" Content="1"/>
</Grid>


推荐答案

使用相对源绑定:

Text="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=Content}"

编辑:我可能应该注意到,就绑定目标而言,这是相当的在 ContentTemplate 中的 {Binding} 作为 DataContext 内容

I probably should note that, in terms of what the binding targets, this is equivalent to {Binding}, as the DataContext in the ContentTemplate is the Content.

但是直接绑定到 DataContext 不会传播回源 DataContext ,因此 ContentControl 的内容 code>将不会在使用此绑定时更改(或双向兼容变体 {Binding。} 据我所知)。

However binding directly to the DataContext will not propagate back to the source DataContext, hence the Content of the ContentControl would not change when when using this binding (or the two-way compliant variation {Binding .}, which changes absolutely nothing as far as I can tell).

这篇关于绑定数据表到ContentControl的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:04