Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. <HierarchicalDataTemplate
  2. DataType="{x:Type local:CheckableItem}"
  3. ItemsSource="{Binding Children}">
  4. <StackPanel Orientation="Horizontal">
  5. <CheckBox IsChecked="{Binding IsChecked}"/>
  6. <TextBlock Text="{Binding Value}"/>
  7. </StackPanel>
  8. </HierarchicalDataTemplate>
  9.  
  10. <TreeView ItemsSource="{Binding MyCollectionOfCheckableItems}"/>
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18.  
  19. namespace WpfApplication102
  20. {
  21. public class Family : DependencyObject
  22. {
  23. public string Name { get; set; }
  24. public List<Person> Members { get; set; }
  25. }
  26.  
  27. public class Person : DependencyObject
  28. {
  29. public string Name { get; set; }
  30. }
  31. }
  32.  
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Diagnostics;
  36. using System.Linq;
  37. using System.Text;
  38. using System.Threading.Tasks;
  39. using System.Windows;
  40.  
  41. namespace WpfApplication102
  42. {
  43. public class ItemHelper : DependencyObject
  44. {
  45. public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.RegisterAttached("IsChecked", typeof(bool?), typeof(ItemHelper), new PropertyMetadata(false, new PropertyChangedCallback(OnIsCheckedPropertyChanged)));
  46. private static void OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  47. {
  48. if (d is Family && ((bool?)e.NewValue).HasValue)
  49. foreach (Person p in (d as Family).Members)
  50. ItemHelper.SetIsChecked(p, (bool?)e.NewValue);
  51.  
  52. if (d is Person)
  53. {
  54. int checked = ((d as Person).GetValue(ItemHelper.ParentProperty) as Family).Members.Where(x => ItemHelper.GetIsChecked(x) == true).Count();
  55. int unchecked = ((d as Person).GetValue(ItemHelper.ParentProperty) as Family).Members.Where(x => ItemHelper.GetIsChecked(x) == false).Count();
  56. if (unchecked > 0 && checked > 0)
  57. {
  58. ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, null);
  59. return;
  60. }
  61. if (checked > 0)
  62. {
  63. ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, true);
  64. return;
  65. }
  66. ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, false);
  67. }
  68. }
  69. public static void SetIsChecked(DependencyObject element, bool? IsChecked)
  70. {
  71. element.SetValue(ItemHelper.IsCheckedProperty, IsChecked);
  72. }
  73. public static bool? GetIsChecked(DependencyObject element)
  74. {
  75. return (bool?)element.GetValue(ItemHelper.IsCheckedProperty);
  76. }
  77.  
  78. public static readonly DependencyProperty ParentProperty = DependencyProperty.RegisterAttached("Parent", typeof(object), typeof(ItemHelper));
  79. public static void SetParent(DependencyObject element, object Parent)
  80. {
  81. element.SetValue(ItemHelper.ParentProperty, Parent);
  82. }
  83. public static object GetParent(DependencyObject element)
  84. {
  85. return (object)element.GetValue(ItemHelper.ParentProperty);
  86. }
  87. }
  88. }
  89.  
  90. <Window x:Class="WpfApplication102.MainWindow"
  91. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  92. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  93. xmlns:local="clr-namespace:WpfApplication102"
  94. Title="MainWindow" Height="220" Width="250">
  95.  
  96. <StackPanel>
  97.  
  98. <TreeView x:Name="treeView" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Families}">
  99. <TreeView.Resources>
  100. <HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Members}" >
  101. <CheckBox Content="{Binding Name}" IsChecked="{Binding Path=(local:ItemHelper.IsChecked), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
  102. <CheckBox.Style>
  103. <Style TargetType="{x:Type CheckBox}">
  104. <Setter Property="Foreground" Value="Black"/>
  105. <Setter Property="Visibility" Value="Visible"/>
  106. <Style.Triggers>
  107. <DataTrigger Binding="{Binding Path=(local:ItemHelper.IsChecked)}" Value="False" >
  108. <Setter Property="Foreground" Value="LightGray"/>
  109. </DataTrigger>
  110. </Style.Triggers>
  111. </Style>
  112. </CheckBox.Style>
  113. </CheckBox>
  114. </HierarchicalDataTemplate>
  115. <DataTemplate DataType="{x:Type local:Person}" >
  116. <CheckBox Content="{Binding Name}" IsChecked="{Binding Path=(local:ItemHelper.IsChecked), Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
  117. <CheckBox.Style>
  118. <Style TargetType="{x:Type CheckBox}">
  119. <Setter Property="Foreground" Value="Black"/>
  120. <Setter Property="Visibility" Value="Visible"/>
  121. <Style.Triggers>
  122. <DataTrigger Binding="{Binding Path=(local:ItemHelper.IsChecked)}" Value="False" >
  123. <Setter Property="Foreground" Value="LightGray"/>
  124. </DataTrigger>
  125. </Style.Triggers>
  126. </Style>
  127. </CheckBox.Style>
  128. </CheckBox>
  129. </DataTemplate>
  130. </TreeView.Resources>
  131. <TreeView.ItemContainerStyle>
  132. <Style TargetType="{x:Type TreeViewItem}">
  133. <Setter Property="IsExpanded" Value="True"/>
  134. </Style>
  135. </TreeView.ItemContainerStyle>
  136. </TreeView>
  137.  
  138. <Button Content="?" Click="Button_PrintCrew_Click" />
  139.  
  140. <TextBlock x:Name="textBoxCrew"/>
  141.  
  142. </StackPanel>
  143.  
  144. </Window>
  145.  
  146. using System;
  147. using System.Collections.Generic;
  148. using System.Collections.ObjectModel;
  149. using System.Diagnostics;
  150. using System.Linq;
  151. using System.Text;
  152. using System.Threading.Tasks;
  153. using System.Windows;
  154. using System.Windows.Controls;
  155. using System.Windows.Data;
  156. using System.Windows.Documents;
  157. using System.Windows.Input;
  158. using System.Windows.Media;
  159. using System.Windows.Media.Imaging;
  160. using System.Windows.Navigation;
  161. using System.Windows.Shapes;
  162.  
  163. namespace WpfApplication102
  164. {
  165. /// <summary>
  166. /// Interaction logic for MainWindow.xaml
  167. /// </summary>
  168. public partial class MainWindow : Window
  169. {
  170. public ObservableCollection<Family> Families { get; set; }
  171.  
  172. public MainWindow()
  173. {
  174. InitializeComponent();
  175.  
  176. this.Families = new ObservableCollection<Family>();
  177. this.Families.Add(new Family() { Name = "Simpsons", Members = new List<Person>() { new Person() { Name = "Homer" }, new Person() { Name = "Bart" } } });
  178. this.Families.Add(new Family() { Name = "Griffin", Members = new List<Person>() { new Person() { Name = "Peter" }, new Person() { Name = "Stewie" } } });
  179. this.Families.Add(new Family() { Name = "Fry", Members = new List<Person>() { new Person() { Name = "Philip J." } } });
  180.  
  181. foreach (Family family in this.Families)
  182. foreach (Person person in family.Members)
  183. person.SetValue(ItemHelper.ParentProperty, family);
  184. }
  185.  
  186. private void Button_PrintCrew_Click(object sender, RoutedEventArgs e)
  187. {
  188. string crew = "";
  189. foreach (Family family in this.Families)
  190. foreach (Person person in family.Members)
  191. if (ItemHelper.GetIsChecked(person) == true)
  192. crew += person.Name + ", ";
  193. crew = crew.TrimEnd(new char[] { ',', ' ' });
  194. this.textBoxCrew.Text = "Your crew: " + crew;
  195. }
  196. }
  197. }
  198.  
  199. interface IParent<T>
  200. {
  201. IEnumerable<T> GetChildren();
  202. }
  203.  
  204. using System;
  205. using System.Collections.Generic;
  206. using System.Windows;
  207.  
  208. public class Family : DependencyObject, IParent<object>
  209. {
  210. public string Name { get; set; }
  211. public List<Person> Members { get; set; }
  212.  
  213. IEnumerable<object> IParent<object>.GetChildren()
  214. {
  215. return Members;
  216. }
  217. }
  218.  
  219. public class Person : DependencyObject
  220. {
  221. public string Name { get; set; }
  222. }
  223.  
  224. using System.Linq;
  225. using System.Windows;
  226.  
  227. public class ItemHelper : DependencyObject
  228. {
  229. public static readonly DependencyProperty IsCheckedProperty =
  230. DependencyProperty.RegisterAttached("IsChecked", typeof(bool?), typeof(ItemHelper),
  231. new PropertyMetadata(false, new PropertyChangedCallback(OnIsCheckedPropertyChanged)));
  232.  
  233. private static void OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  234. {
  235. IParent<object> sect = d as IParent<object>;
  236. DependencyObject depObj = d as DependencyObject;
  237.  
  238. if (sect != null)
  239. {
  240. if (((bool?)e.NewValue).HasValue)
  241. {
  242. foreach (DependencyObject p in sect.GetChildren())
  243. {
  244. SetIsChecked(p, (bool?)e.NewValue);
  245. }
  246. }
  247. }
  248.  
  249. if (depObj != null)
  250. {
  251. var parentObject = depObj.GetValue(ParentProperty) as IParent<object>;
  252. var parentDO = depObj.GetValue(ParentProperty) as DependencyObject;
  253. int ch = parentObject?.GetChildren()?.Where(
  254. x => GetIsChecked(x as DependencyObject) == true).Count() ?? 0;
  255. int un = parentObject?.GetChildren()?.Where(
  256. x => GetIsChecked(x as DependencyObject) == false).Count() ?? 0;
  257. if (un > 0 && ch > 0)
  258. {
  259. SetIsChecked(parentDO, null);
  260. return;
  261. }
  262. if (ch > 0)
  263. {
  264. SetIsChecked(parentDO, true);
  265. return;
  266. }
  267. SetIsChecked(parentDO, false);
  268. }
  269. }
  270. public static void SetIsChecked(DependencyObject element, bool? IsChecked)
  271. {
  272. element?.SetValue(IsCheckedProperty, IsChecked);
  273. }
  274. public static bool? GetIsChecked(DependencyObject element)
  275. {
  276. return (bool?)element?.GetValue(IsCheckedProperty);
  277. }
  278.  
  279. public static readonly DependencyProperty ParentProperty =
  280. DependencyProperty.RegisterAttached("Parent", typeof(object), typeof(ItemHelper));
  281.  
  282. public static void SetParent(DependencyObject element, object Parent)
  283. {
  284. element?.SetValue(ParentProperty, Parent);
  285. }
  286. public static object GetParent(DependencyObject element)
  287. {
  288. return element?.GetValue(ParentProperty);
  289. }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement