WPF DataTemplate绑定 [英] WPF DataTemplate Binding
问题描述
我已经设置了一个小例子来说明:
< Window x:Class =HAND.BindingExample
xmlns =http://schemas.microsoft.com/ winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =BindingExampleHeight =506Width = 656
DataContext ={Binding RelativeSource = {RelativeSource Self}}
>
< Grid>
< TabControl HorizontalAlignment =LeftHeight =381VerticalAlignment =TopWidth =608>
< TabItem Header =TabItem>
< Label Content ={Binding Path = myString}/>
< / TabItem>
< TabItem Header =TabItem>
< TabItem.ContentTemplate>
< DataTemplate>
< Label Content ={Binding Path = myString}/>
< / DataTemplate>
< /TabItem.ContentTemplate>
< / TabItem>
< / TabControl>
< / Grid>
< / Window>
Tab1按预期工作,Tab2为空。
后面的代码:
使用System.Windows;
命名空间HAND
{
public partial class BindingExample:Window
{
public string myString {get;组; }
public BindingExample()
{
myString =Hello Stackoverflow;
InitializeComponent();
}
}
}
您正在使用 ContentTemplate
属性。从 ContentControl.ContentTemplate
MSDN上的属性页面
获取或设置用于显示ContentControl内容的数据模板。
因此,设置此属性时,还需要设置内容
属性到某种数据源:
< TabControl>
< TabItem Header =TabItem>
< Label Content ={Binding Path = myString}/>
< / TabItem>
< TabItem Header =TabItemContent ={Binding Path = myString}>
< TabItem.ContentTemplate>
< DataTemplate>
< Label Content ={Binding}/>
< / DataTemplate>
< /TabItem.ContentTemplate>
< / TabItem>
< / TabControl>
I discovered when using a ContentTemplate/DataTemplate in a WPF TabControl my Bindings will not work anymore.
I have set up a small example to illustrate:
<Window x:Class="HAND.BindingExample"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="BindingExample" Height="506" Width="656"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<Grid>
<TabControl HorizontalAlignment="Left" Height="381" VerticalAlignment="Top" Width="608">
<TabItem Header="TabItem">
<Label Content="{Binding Path=myString}"/>
</TabItem>
<TabItem Header="TabItem">
<TabItem.ContentTemplate>
<DataTemplate>
<Label Content="{Binding Path=myString}"/>
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
</TabControl>
</Grid>
</Window>
Tab1 works as expected, Tab2 is empty.
the code behind:
using System.Windows;
namespace HAND
{
public partial class BindingExample : Window
{
public string myString { get; set; }
public BindingExample()
{
myString = "Hello Stackoverflow";
InitializeComponent();
}
}
}
You are using the ContentTemplate
property incorrectly. From the ContentControl.ContentTemplate
Property page on MSDN:
Gets or sets the data template used to display the content of the ContentControl.
Therefore, when setting this property, you also need to set the Content
property to some sort of data source:
<TabControl>
<TabItem Header="TabItem">
<Label Content="{Binding Path=myString}"/>
</TabItem>
<TabItem Header="TabItem" Content="{Binding Path=myString}">
<TabItem.ContentTemplate>
<DataTemplate>
<Label Content="{Binding}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
</TabControl>
这篇关于WPF DataTemplate绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!