WPF DataTemplate绑定 [英] WPF DataTemplate Binding

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

问题描述

我发现在WPF TabControl中使用ContentTemplate / DataTemplate时,我的绑定将不再工作。



我已经设置了一个小例子来说明:

 < 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 Horizo​​ntalAlignment =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屋!

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