Search code examples
c#wpfmvvmdata-bindingtabcontrol

Tabcontrol: Is It possible to let Controls from different Tabitems comunicate with each other?


Is it possible to let Controls from different Tabitems comunicate with each other ? I made a lot of research but i wasn't able to find anything that could help me so i started to wonder if its even Possible to let 2 Controls of 2 different Tabitems inside a Tabcontrol communicate with each other.

In my case i want to fire a command from a Button that is on Tabitem A to change the string shown in a Textbox on Tabitem B.

Every kind of help or an example would be much appreciated.

How I create my Tabs (example)

<TabItem  DataContext="{Binding TypenschildVM , Source= {StaticResource vm_Für_Typenschild} }"  Loaded="Typenschild_Loaded"  FontSize="15"  Height="50" Foreground="White"  FontWeight="Bold"  Background="#401746" Header="Typenschild" BorderBrush="#FFACACAC" Margin="56,0,-119,0">
        <view:TypenschildV    Width="Auto" Height="Auto"  HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Margin="1,0"/>
    </TabItem>

My Singelton Model (every other model inherits from it )

 public  class DateiM 
{
    private static DateiM instance = null;
    private static readonly object padlock = new object();
    public string XMLDatei
    {
        get;
        set;
    }

    public static DateiM Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new DateiM();
                }
                return instance;
            }
        }
    }
}

Model for View A

 public class TypenschildM : DateiM
    {
        public DateiM datei = DateiM.Instance;
        private static TypenschildM instance = null;
        private static readonly object padlock = new object();

        public  string Hardwareversion { get; set; } 
        public  string Maschinennummer { get; set; }
        public  string Seriennummer { get; set; }
        public  string Kundennummer { get; set; }
        public  string Datum_Auslieferung { get; set; }
        public  string Formatkennung { get; set; }
        public  string Gewicht { get; set; }
        public  string Versorgungsdruck { get; set; }
        public static TypenschildM Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new TypenschildM();
                    }
                
                    return instance;
                }
            }
        }
        public TypenschildM()
        {
            //Hardwareversion =  PropTest;
        }
    }    

Databinding of the Property that i want to show

 <TextBox  Text="{Binding PropTest, Source={StaticResource vm_Für_Typenschild},  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  x:Name="textBoxTyp" Height="22" Margin="243,93,133,0" TextWrapping="Wrap"  VerticalAlignment="Top" Background="#FFF3F3F3" Grid.Column="2"/>

Solution

  • This is not about communication between controls but more about your internal 'business' logic of the data models. Basically you're saying you want to change a string in one model (shown in Textbox on Tab B) to change when a button is clicked (button anywhere, in your case on Tab A). The string should change in the GUI not because you communicate between tabs but because the string in the underlying datamodel has changed and the binding mechanism has notified the view to change.

    With that said, how do you write the logic of changing a property in one datamodel based on a callback from another? That's pretty much up to you but a common mechanism might be to use a messenger class or mediator design pattern.

    An example: a button is bound to an ICommand in a ViewModel and the ViewModel then notifies the rest of your app of the change (either changing the string property directly, or sending a message that would be handled by the model, or triggering a mediator etc.). Then when the string property changes in the underlying datamodel, the INotifyPropertyChanged mechanism kicks in and the text in the text field is updated.