Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF数据模板绑定_C#_Wpf_Data Binding_Datatemplate - Fatal编程技术网

C# WPF数据模板绑定

C# WPF数据模板绑定,c#,wpf,data-binding,datatemplate,C#,Wpf,Data Binding,Datatemplate,我发现在WPF选项卡控件中使用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="Bin

我发现在WPF选项卡控件中使用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="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>

但正如你所知道的,把一扇窗户绑在自己身上,并不是一个好办法。
我不知道您是否只是为了示例而这样做,但如果不这样做,请尝试创建一个适当的viewModel来绑定窗口;)

您错误地使用了
ContentTemplate
属性。从MSDN上的页面:

获取或设置用于显示ContentControl内容的数据模板

因此,在设置此属性时,还需要将
Content
属性设置为某种数据源:

<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>


我让它在我的机器上运行。也许当你把窗户绑在自己身上的时候它就不会运行了,我没有这样做。谢谢!我的例子就是这样。我在使用MVVM模式的真实程序中使用了不同的绑定类。我仍然不明白为什么你的代码有效而我的代码无效。现在我将尝试将示例的解决方案应用到我的应用程序中。Sheridan发布了一个答案,其XAML代码与我的相同,但对ContentTemplate的工作原理有了更好的解释。这应该有助于你们理解是的,谢谢你们两个!嗨,miiite,你的答案对我有用,测试错了。对你的答案进行编辑,我会将下一个结论还原为Miiite。非常感谢你的解释!为什么需要在中设置Content=“{Binding Path=mystring}”,然后在中设置Content=“{Binding}”?在本例中,TabItem.ContentTemplate似乎根本不需要它?如果没有模板,它不会工作得很好吗?我试图理解模板以及如何将标签的其他属性绑定到父级的DataContext。。。谢谢我们将
myString
属性绑定到
Content
属性以赋予它一些值。在这里,该值只是一个字符串,但它可能是具有其他属性的对象。在
ContentTemplate
中,我们告诉它要在
标签中显示数据绑定对象的哪一部分。在本例中,我们希望绑定到整个字符串对象,因此我们使用
{Binding}
来实现这一点。但不是这样:该示例仅适用于只读绑定-如果您将
Label
更改为
TextBox
,myString将永远不会被写回。
<TabItem Content="{Binding myString}" Header="TabItem">
    <TabItem.ContentTemplate>
        <DataTemplate>
            <Label Content="{Binding}" />
        </DataTemplate>
    </TabItem.ContentTemplate>
</TabItem>
<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>