天天看點

WPF效果第二百一十一篇之TreeView勾選

作者:opendotnet

前面已經實作了WPF效果第二百零二篇之TreeView帶連接配接線,給專家展示,結果人家有點不滿意

WPF效果第二百一十一篇之TreeView勾選

;然後提了個要求:能不能每個節點都能讓我随心所欲的勾選;咱好像啥也不能說,隻能盡量滿足就行了

WPF效果第二百一十一篇之TreeView勾選

;二話不多說直接看效果:

WPF效果第二百一十一篇之TreeView勾選

1、前台關于ItemTemplate如下:

<HierarchicalDataTemplate x:Key="LedDataTemplate" DataType="{x:Type Models:TreeModel}" ItemsSource="{Binding Children,Mode=TwoWay}">              <WrapPanel Orientation="Horizontal" Margin="10">              <!--支援勾選-->              <CheckBox Style="{StaticResource TickCheckBox}" IsChecked="{Binding IsChecked}" />              <Path Data="M138.66666,63.999992 C98.133335,63.999992 64.000004,98.133339 64.000004,138.66666 L64.000004,539.73335 C64.000004,580.26667 98.133335,614.39997 138.66666,614.39997 L793.60003,614.39997 C834.13335,614.39997 868.26665,580.26667 868.26665,539.73335 L868.26665,138.66666 C868.26665,98.133339 834.13335,63.999992 793.60003,63.999992 z M138.66666,0 L793.60003,0 C870.40001,0 932.26665,61.866661 932.26665,138.66666 L932.26665,539.73335 C932.26665,616.53333 870.40001,678.39997 793.60003,678.39997 L499.2,678.39997 499.2,785.06666 723.2,785.06666 C740.26665,785.06666 755.2,800.00001 755.2,817.06666 755.2,834.13331 740.26665,849.06666 723.2,849.06666 L211.2,849.06666 C194.13334,849.06666 179.2,834.13331 179.2,817.06666 179.2,800.00001 194.13334,785.06666 211.2,785.06666 L435.2,785.06666 435.2,678.39997 138.66666,678.39997 C61.866665,678.39997 0,616.53333 0,539.73335 L0,138.66666 C0,61.866661 61.866665,0 138.66666,0 z" Stretch="Fill" Width="16" Height="16" Fill="White" Visibility="{Binding ItemType,Converter={StaticResource ItemTypeEnumToPathVisibilityConvert}}" />              <TextBlock x:Name="ShowTbk" Margin="4,0,0,0" Text="{Binding Name,Mode=TwoWay}" Foreground="White" FontSize="{Binding ItemType,Converter={StaticResource ItemTypeEnumToFontSizeConvert}}" />              </WrapPanel>              </HierarchicalDataTemplate>           

2、背景關于勾選通過IsChecked屬性控制:

private void SetIsChecked(bool ? value, bool updateChildren, bool updateParent)              {              if(value != isChecked)              {              isChecked = value;              if(updateChildren && isChecked.HasValue)              {              Children.ToList().ForEach(c => c.SetIsChecked(isChecked, true, false));              }              if(updateParent && _parent != )              {              _parent.VerifyCheckState();              }              OnPropertyChanged("IsChecked");              }              }           

3、關于VerifyCheckState方法:

private void VerifyCheckState()              {              int count = Children.Where(x => x.IsChecked == false).Count();              bool ? state = count > 0 ? false : true;              SetIsChecked(state, false, true);              }           

最終簡單的效果先這樣吧

WPF效果第二百一十一篇之TreeView勾選

;以後有時間的話,可以再去摸索一下更複雜的效果

WPF效果第二百一十一篇之TreeView勾選

;程式設計不息、Bug不止、無Bug、無生活;改bug的冷靜、編碼的激情、完成後的喜悅、挖坑的激動 、填坑的興奮;這也許就是屌絲程式員的樂趣吧;今天就到這裡吧;希望自己有動力一步一步堅持下去;生命不息,代碼不止;大家抽空可以看看今天分享的效果,有好的意見和想法,可以在留言闆随意留言;我看到後會第一時間回複大家,多謝大家的一直默默的關注和支援!