Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. public class MainWindowViewModel : INotifyPropertyChanged
  2. {
  3. // in the real implementation these are not created on the fly but fetched elsewhere
  4. private ObservableCollection<A> _Adata;
  5. public ObservableCollection<A> AData { get { return _Adata; } }
  6.  
  7. private ObservableCollection<B> _Bdata;
  8. public ObservableCollection<B> BData { get { return _Bdata; } }
  9.  
  10. public ICollectionView AView, BView;
  11.  
  12. private A _currSelectedA;
  13. public A CurrSelectedA
  14. {
  15. get { return _currSelectedA; }
  16. private set
  17. {
  18. _currSelectedA = value;
  19. OnPropertyChanged("CurrentSelectedA");
  20. }
  21. }
  22.  
  23. private ICommand _rowClickCommand;
  24.  
  25. public MainWindowViewModel()
  26. {
  27.  
  28. _rowClickCommand = new MainWindow.DelegateCommand(() =>
  29. {
  30. // This command gets correctly executed, yet it does not produce any visible result in the View
  31. var complexTypeB = new ComplexTypeB();
  32. _Bdata = new ObservableCollection<B>(complexTypeB.l);
  33. BView = CollectionViewSource.GetDefaultView(_Bdata);
  34. });
  35.  
  36. var someComplexTypeInstance = new ComplexTypeA();
  37. _Adata = new ObservableCollection<A>(someComplexTypeInstance.l);
  38.  
  39. AView = CollectionViewSource.GetDefaultView(_Adata);
  40. // At this point, the UI correctly shows the table
  41.  
  42. AView.CurrentChanged += delegate
  43. {
  44. _currSelectedA = (A)AView.CurrentItem;
  45. };
  46. }
  47.  
  48. public ICommand RowClickCommand
  49. {
  50. get { return _rowClickCommand; }
  51. }
  52.  
  53. public event PropertyChangedEventHandler PropertyChanged;
  54. protected void OnPropertyChanged(string propertyName)
  55. {
  56. if (PropertyChanged != null)
  57. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  58. }
  59. }
  60.  
  61. public class A { /* ... */ }
  62. public class B { /* ... */ }
  63.  
  64. public class ComplexTypeA
  65. {
  66. public List<A> l = new List<A>();
  67. }
  68. public class ComplexTypeB
  69. {
  70. public List<B> l = new List<B>();
  71. }
  72.  
  73. public class MainWindow : Window
  74. {
  75. public MainWindow()
  76. {
  77. DataContext = new MainWindowViewModel();
  78. }
  79.  
  80. public class DelegateCommand : ICommand
  81. {
  82. private readonly Action _action;
  83. public DelegateCommand(Action action) { _action = action; }
  84.  
  85. public bool CanExecute(object parameter) {return true; }
  86.  
  87. public void Execute(object parameter) { _action(); }
  88.  
  89. public event EventHandler CanExecuteChanged;
  90. public void OnCanExecuteChanged()
  91. {
  92. CanExecuteChanged(this, EventArgs.Empty);
  93. }
  94. }
  95. }
  96.  
  97. <Window x:Class="WpfApplication.MainWindow"
  98. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  99. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  100. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  101. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  102. xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  103. xmlns:local="clr-namespace:WpfApplication"
  104. mc:Ignorable="d"
  105. d:DesignHeight="300" d:DesignWidth="300"
  106. Title="MainWindow">
  107. <Grid d:DataContext="{d:DesignInstance Type=local:MainWindowViewModel}">
  108.  
  109. <Grid.RowDefinitions>
  110. <RowDefinition/>
  111. <RowDefinition/>
  112. </Grid.RowDefinitions>
  113.  
  114. <Grid Grid.Row="0">
  115. <DataGrid
  116. x:Name="ADataGrid"
  117. HorizontalAlignment="Left"
  118. VerticalAlignment="Top"
  119. SelectedItem="{Binding CurrSelectedA, Mode=TwoWay}"
  120. ItemsSource="{Binding AData}">
  121. <i:Interaction.Triggers>
  122. <i:EventTrigger EventName="MouseLeftButtonUp" >
  123. <i:InvokeCommandAction
  124. Command="{Binding RowClickCommand}"/>
  125. </i:EventTrigger>
  126. </i:Interaction.Triggers>
  127. </DataGrid>
  128. </Grid>
  129.  
  130. <Grid Grid.Row="1">
  131. <DataGrid
  132. x:Name="BDataGrid"
  133. HorizontalAlignment="Left"
  134. VerticalAlignment="Top"
  135. ItemsSource="{Binding BData}"/>
  136. </Grid>
  137.  
  138. </Grid>
  139. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement