数据绑定问题在TabControl的ContentTemplate中 [英] DataBinding Problem In ContentTemplate of a TabControl

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

问题描述

我有麻烦的数据绑定的tabControl的内容模板。

I am in a trouble with databinding of the tabControl's Content Template.

我有这个类

public class MainWindowViewModel : INotifyPropertyChanged
    {
        string _text1 = "text1";
        string _text2 = "text2";
        string _text3 = "text3";

        public string Text1 
        {
            get
            {
                return _text1;
            }
            set
            {
                _text1 = value;
            }
        }

        public string Text2
        {
            get
            {
                return _text2;
            }
            set
            {
                _text2 = value;
            }
        }

        public string Text3
        {
            get
            {
                return _text3;
            }
            set
            {
                _text3 = value;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

我有那个xaml:

<Window x:Class="LazyBindingTabControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:LazyBindingTabControl"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl Height="299" Margin="0,12,0,0" Name="tabControl1" VerticalAlignment="Top">
            <TabItem Header="tabItem1" Name="tabItem1">
                <TabItem.ContentTemplate>
                    <DataTemplate>
                        <TextBlock Name="Text1" Text="{Binding Path=DataContext.Text1}" />
                    </DataTemplate>
                </TabItem.ContentTemplate>
            </TabItem>
            <TabItem Header="tabItem2" Name="tabItem2">
                <TabItem.ContentTemplate>
                    <DataTemplate>
                        <TextBlock Name="Text2" Text="{Binding Path=DataContext.Text2}" />
                    </DataTemplate>
                </TabItem.ContentTemplate>
            </TabItem>
            <TabItem Header="tabItem3" Name="tabItem3">
                <TabItem.ContentTemplate>
                    <DataTemplate>
                        <TextBlock Name="Text3" Text="{Binding Path=DataContext.Text3}" />
                    </DataTemplate>
                </TabItem.ContentTemplate>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

另外我还在mainWindow.xaml的code-behinf中设置了dataContext

And also I set dataContext in code-behinf of mainWindow.xaml

namespace LazyBindingTabControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowViewModel();
        }
    }
}

但绑定不成功。如何正确绑定Text1,Text2和Text3属性。

But binding is not successful. How can I successfuly bind Text1, Text2 and Text3 properties properly.

谢谢。

推荐答案

将tabitem内容绑定到Text1,Text2,Text3属性

Bind the tabitem content to Text1, Text2, Text3 Properties

然后在TextBlock部分,绑定这样的文本

then in the TextBlock part, bind the Text like this

Text={Binding}

所以代码就像这样

   <TabItem Header="tabItem1" Name="tabItem1" Content="{Binding Text1}">
      <TabItem.ContentTemplate>
        <DataTemplate>
          <TextBlock Name="Text1" Text="{Binding}" />
        </DataTemplate>
      </TabItem.ContentTemplate>
    </TabItem>

应该工作

这篇关于数据绑定问题在TabControl的ContentTemplate中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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