Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="wpfTest1.MainWindow"
- xmlns:local="clr-namespace:wpfTest1"
- Title="MainWindow" Height="350" Width="525">
- <Window.Resources>
- <local:MyData x:Key="myData"/>
- <DataTemplate x:Key="tabTemplate">
- <StackPanel DataContext="{StaticResource myData}">
- <TextBox Text="{Binding Value1}"/>
- <TextBox Text="{Binding Value2}"/>
- </StackPanel>
- </DataTemplate>
- </Window.Resources>
- <Grid>
- <TabControl x:Name="tbPanel" SelectionChanged="tbPanel_SelectionChanged">
- <TabItem Header="Object A" ContentTemplate="{StaticResource tabTemplate}" />
- <TabItem Header="Object B" ContentTemplate="{StaticResource tabTemplate}" />
- </TabControl>
- </Grid>
- </Window>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace wpfTest1
- {
- /// <summary>
- /// Interaction logic for MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- MyData myData = null;
- MyData objectA = new MyData();
- MyData objectB = new MyData();
- public MainWindow()
- {
- InitializeComponent();
- myData = ((MyData)this.Resources["myData"]);
- if (myData == null)
- {
- Application.Current.Shutdown();
- }
- }
- private void tbPanel_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- TabItem t = (TabItem)tbPanel.SelectedItem;
- if (t.Header.ToString() == "Object A")
- {
- objectB.LoadDataFromObject(myData);
- myData.LoadDataFromObject(objectA);
- }
- else if (t.Header.ToString() == "Object B")
- {
- objectA.LoadDataFromObject(myData);
- myData.LoadDataFromObject(objectB);
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace wpfTest1
- {
- public class MyData : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- int value1 = 0, value2 = 0;
- public int Value1
- {
- get { return value1; }
- set
- {
- if (value1 != value)
- {
- value1 = value;
- NotifyProperyChanged("Value1");
- }
- }
- }
- public int Value2
- {
- get { return value2; }
- set
- {
- if (value2 != value)
- {
- value2 = value;
- NotifyProperyChanged("Value2");
- }
- }
- }
- private void NotifyProperyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- public void LoadDataFromObject(MyData m)
- {
- this.Value1 = m.Value1;
- this.Value2 = m.Value2;
- }
- public MyData() { }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement