Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="wpfTest1.MainWindow"
  6. xmlns:local="clr-namespace:wpfTest1"
  7. Title="MainWindow" Height="350" Width="525">
  8. <Window.Resources>
  9. <local:MyData x:Key="myData"/>
  10. <DataTemplate x:Key="tabTemplate">
  11. <StackPanel DataContext="{StaticResource myData}">
  12. <TextBox Text="{Binding Value1}"/>
  13. <TextBox Text="{Binding Value2}"/>
  14. </StackPanel>
  15. </DataTemplate>
  16. </Window.Resources>
  17. <Grid>
  18. <TabControl x:Name="tbPanel" SelectionChanged="tbPanel_SelectionChanged">
  19. <TabItem Header="Object A" ContentTemplate="{StaticResource tabTemplate}" />
  20. <TabItem Header="Object B" ContentTemplate="{StaticResource tabTemplate}" />
  21. </TabControl>
  22. </Grid>
  23. </Window>
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Text;
  29. using System.Threading.Tasks;
  30. using System.Windows;
  31. using System.Windows.Controls;
  32. using System.Windows.Data;
  33. using System.Windows.Documents;
  34. using System.Windows.Input;
  35. using System.Windows.Media;
  36. using System.Windows.Media.Imaging;
  37. using System.Windows.Navigation;
  38. using System.Windows.Shapes;
  39.  
  40. namespace wpfTest1
  41. {
  42. /// <summary>
  43. /// Interaction logic for MainWindow.xaml
  44. /// </summary>
  45. public partial class MainWindow : Window
  46. {
  47.  
  48. MyData myData = null;
  49.  
  50. MyData objectA = new MyData();
  51. MyData objectB = new MyData();
  52.  
  53. public MainWindow()
  54. {
  55. InitializeComponent();
  56.  
  57. myData = ((MyData)this.Resources["myData"]);
  58.  
  59. if (myData == null)
  60. {
  61. Application.Current.Shutdown();
  62. }
  63.  
  64. }
  65.  
  66. private void tbPanel_SelectionChanged(object sender, SelectionChangedEventArgs e)
  67. {
  68. TabItem t = (TabItem)tbPanel.SelectedItem;
  69. if (t.Header.ToString() == "Object A")
  70. {
  71. objectB.LoadDataFromObject(myData);
  72. myData.LoadDataFromObject(objectA);
  73. }
  74. else if (t.Header.ToString() == "Object B")
  75. {
  76. objectA.LoadDataFromObject(myData);
  77. myData.LoadDataFromObject(objectB);
  78. }
  79. }
  80. }
  81. }
  82.  
  83. using System;
  84. using System.Collections.Generic;
  85. using System.ComponentModel;
  86. using System.Linq;
  87. using System.Text;
  88. using System.Threading.Tasks;
  89.  
  90. namespace wpfTest1
  91. {
  92. public class MyData : INotifyPropertyChanged
  93. {
  94. public event PropertyChangedEventHandler PropertyChanged;
  95.  
  96. int value1 = 0, value2 = 0;
  97.  
  98. public int Value1
  99. {
  100. get { return value1; }
  101. set
  102. {
  103. if (value1 != value)
  104. {
  105. value1 = value;
  106. NotifyProperyChanged("Value1");
  107. }
  108. }
  109. }
  110.  
  111. public int Value2
  112. {
  113. get { return value2; }
  114. set
  115. {
  116. if (value2 != value)
  117. {
  118. value2 = value;
  119. NotifyProperyChanged("Value2");
  120. }
  121. }
  122. }
  123.  
  124. private void NotifyProperyChanged(string propertyName)
  125. {
  126. if (PropertyChanged != null)
  127. {
  128. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  129. }
  130. }
  131.  
  132. public void LoadDataFromObject(MyData m)
  133. {
  134. this.Value1 = m.Value1;
  135. this.Value2 = m.Value2;
  136. }
  137.  
  138. public MyData() { }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement