I want to have a treeview. I got help with this article
Working with Checkboxes in the WPF TreeView[
^], but I encountered two problems.
What I have tried:
Error picture
http://uupload.ir/files/6jg5_error_1.jpg[
^]
http://uupload.ir/files/cq8k_error_2.jpg[
^]
XAML :
<Window x:Class="DamDarYar_IDMS.WinDastresi"
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"
xmlns:local="clr-namespace:DamDarYar_IDMS"
mc:Ignorable="d"
Title="WinDastresi" Height="300" Width="300" WindowStartupLocation="CenterScreen" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\themes\royale.normalcolor.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="TreeViewUtemStyle" TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True"/>
<Setter Property="IsSelected" Value="{Binding IsInitiallySelected, Mode=OneTime}"/>
<Setter Property="KeyboardNavigation.AcceptsReturn" Value="True"/>
<Setter Property="VirtualToggleButton.IsVirtualToggleButton" Value="True" />
<Setter Property="VirtualToggleButton.IsChecked" Value="{Binding IsChecked}" />
</Style>
<HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding Children, Mode=OneTime}">
<StackPanel Orientation="Horizontal">
<CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center"/>
<ContentPresenter Content="{Binding BindingGroupName, Mode=OneTime}" Margin="2,0"/>
</StackPanel>
</HierarchicalDataTemplate>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<ObjectDataProvider MethodName="CreatFoos" ObjectType="{x:Type local:FooViewModel}"/>
</Window.DataContext>
<DockPanel>
<Button DockPanel.Dock="Bottom" Command="Undo" Content="حذف همه انتخاب ها" HorizontalAlignment="Center" Margin="0,2" Padding="8,0" FontFamily="B Koodak"/>
<TreeView x:Name="tree" ItemsSource="{Binding Mode=OneTime}" ItemTemplate="{StaticResource CheckBoxItemTemplate}"/>
</DockPanel>
</Window>
XAML.CS
using System.Windows;
using System.Windows.Input;
namespace DamDarYar_IDMS
{
public partial class WinDastresi : Window
{
public WinDastresi()
{
InitializeComponent();
FooViewModel root = this.tree.Items[0] as FooViewModel;
base.CommandBindings.Add(
new CommandBinding(
ApplicationCommands.Undo,
(sender, e) => {
e.Handled = true;
root.IsChecked = false;
this.tree.Focus();
},
(sender, e) => {
e.Handled = true;
e.CanExecute = (root.IsChecked != false);
}));
this.tree.Focus();
}
}
}
using System;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace DamDarYar_IDMS
{
public static class VirtualToggleButton
{
#region attached properties
#region IsChecked
public static readonly System.Windows.DependencyProperty IsCheckedProperty =
DependencyProperty.RegisterAttached("IsChecked", typeof(Nullable<bool>), typeof(VirtualToggleButton),
new FrameworkPropertyMetadata((Nullable<bool>)false,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal,
new PropertyChangedCallback(OnIsCheckedChanged)));
public static Nullable<bool> GetIsChecked(DependencyObject d)
{
return (Nullable<bool>)d.GetValue(IsCheckedProperty);
}
public static void SetIsChecked(DependencyObject d, Nullable<bool> value)
{
d.SetValue(IsCheckedProperty, value);
}
private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement pseudobutton = d as UIElement;
if (pseudobutton != null)
{
Nullable<bool> newValue = (Nullable<bool>)e.NewValue;
if (newValue == true)
{
RaiseCheckedEvent(pseudobutton);
}
else if (newValue == false)
{
RaiseUncheckedEvent(pseudobutton);
}
else
{
RaiseIndeterminateEvent(pseudobutton);
}
}
}
#endregion
#region IsThreeState
public static readonly DependencyProperty IsThreeStateProperty =
DependencyProperty.RegisterAttached("IsThreeState", typeof(bool), typeof(VirtualToggleButton),
new FrameworkPropertyMetadata((bool)false));
public static bool GetIsThreeState(DependencyObject d)
{
return (bool)d.GetValue(IsThreeStateProperty);
}
public static void SetIsThreeState(DependencyObject d, bool value)
{
d.SetValue(IsThreeStateProperty, value);
}
#endregion
#region IsVirtualToggleButton
public static readonly DependencyProperty IsVirtualToggleButtonProperty =
DependencyProperty.RegisterAttached("IsVirtualToggleButton", typeof(bool), typeof(VirtualToggleButton),
new FrameworkPropertyMetadata((bool)false,
new PropertyChangedCallback(OnIsVirtualToggleButtonChanged)));
public static bool GetIsVirtualToggleButton(DependencyObject d)
{
return (bool)d.GetValue(IsVirtualToggleButtonProperty);
}
public static void SetIsVirtualToggleButton(DependencyObject d, bool value)
{
d.SetValue(IsVirtualToggleButtonProperty, value);
}
private static void OnIsVirtualToggleButtonChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
IInputElement element = d as IInputElement;
if (element != null)
{
if ((bool)e.NewValue)
{
element.MouseLeftButtonDown += OnMouseLeftButtonDown;
element.KeyDown += OnKeyDown;
}
else
{
element.MouseLeftButtonDown -= OnMouseLeftButtonDown;
element.KeyDown -= OnKeyDown;
}
}
}
#endregion
#endregion
#region routed events
#region Checked
internal static RoutedEventArgs RaiseCheckedEvent(UIElement target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = ToggleButton.CheckedEvent;
RaiseEvent(target, args);
return args;
}
#endregion
#region Unchecked
internal static RoutedEventArgs RaiseUncheckedEvent(UIElement target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = ToggleButton.UncheckedEvent;
RaiseEvent(target, args);
return args;
}
#endregion
#region Indeterminate
internal static RoutedEventArgs RaiseIndeterminateEvent(UIElement target)
{
if (target == null) return null;
RoutedEventArgs args = new RoutedEventArgs();
args.RoutedEvent = ToggleButton.IndeterminateEvent;
RaiseEvent(target, args);
return args;
}
#endregion
#endregion
#region private methods
private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
UpdateIsChecked(sender as DependencyObject);
}
private static void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.OriginalSource == sender)
{
if (e.Key == Key.Space)
{
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) return;
UpdateIsChecked(sender as DependencyObject);
e.Handled = true;
}
else if (e.Key == Key.Enter && (bool)(sender as DependencyObject).GetValue(KeyboardNavigation.AcceptsReturnProperty))
{
UpdateIsChecked(sender as DependencyObject);
e.Handled = true;
}
}
}
private static void UpdateIsChecked(DependencyObject d)
{
Nullable<bool> isChecked = GetIsChecked(d);
if (isChecked == true)
{
SetIsChecked(d, GetIsThreeState(d) ? (Nullable<bool>)null : (Nullable<bool>)false);
}
else
{
SetIsChecked(d, isChecked.HasValue);
}
}
private static void RaiseEvent(DependencyObject target, RoutedEventArgs args)
{
if (target is UIElement)
{
(target as UIElement).RaiseEvent(args);
}
else if (target is ContentElement)
{
(target as ContentElement).RaiseEvent(args);
}
}
#endregion
}
}