DataBinding Problem In ContentTemplate of a TabControl
Asked Answered
O

2

1

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

I have that class

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));
            }
        }
    }

And I have that 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>

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();
        }
    }
}

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

Thanks.

Ophthalmology answered 18/8, 2010 at 8:11 Comment(0)
P
2

Bind the tabitem content to Text1, Text2, Text3 Properties

then in the TextBlock part, bind the Text like this

Text={Binding}

so the code would be like this

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

that should work

Pointless answered 18/8, 2010 at 9:44 Comment(0)
K
0

I changed your xaml and ViewModel to what it should be to work

ViewModel: When the property is set the NotifyPropertyChanged must be called. I Added 1 example of a property, do it with all 3

    public string Text1
    {
        get
        {
            return _text1;
        }
        set
        {
            _text1 = value;
            NotifyPropertyChanged("Text1");
        }
    }

xaml:

Change the databinding to:

 Text="{Binding Path=Text1}"

After these changes it should work.

Kellikellia answered 18/8, 2010 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.