I am trying to create a program . But I am running in a basic issue.
What I have tried:
xaml
<Window x:Class="DamDarYar_IDMS.Window3"
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="Window3" Height="300" Width="300">
<StackPanel>
<TreeView Name="treeview" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=Families}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Members}">
<CheckBox Content="{Binding Name}" IsChecked="{Binding Path=(local:ItemHelper.Ischecked),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(local:ItemHelper.Ischecked)}" Value="false">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Person}">
<CheckBox Content="{Binding Name}" IsChecked="{Binding Path=(local:ItemHelper.Ischecked),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(local:ItemHelper.Ischecked)}" Value="false">
<Setter Property="Foreground" Value="LightGray"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<Button Content="?" Click="Button_PintCrew_Click"/>
<TextBox Name="textBoxCrew"/>
</StackPanel>
</Window>
xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace DamDarYar_IDMS
{
public partial class Window3 : Window
{
public ObservableCollection<Family> Families { get; set; }
public Window3()
{
InitializeComponent();
this.Families = new ObservableCollection<Family>();
this.Families.Add(new Family() { Name = "Simpsons", Members = new List<Person>() { new Person() { Name = "Homer" }, new Person() { Name = "Baret" } } });
this.Families.Add(new Family() { Name = "Griffin", Members = new List<Person>() { new Person() { Name = "Peter" }, new Person() { Name = "Stewie" } } });
this.Families.Add(new Family() { Name = "Fry", Members = new List<Person>() { new Person() { Name = "Philip J." } } });
foreach (Family family in this.Families)
foreach (Person person in family.Members)
person.SetValue(ItemHelper.ParentProperty, family);
}
private void Button_PintCrew_Click(object sender, RoutedEventArgs e)
{
string crew = "";
foreach (Family family in this.Families)
foreach (Person person in family.Members)
if (ItemHelper.GetIsChecked(person) == true)
crew += person.Name + ",";
crew = crew.TrimEnd(new char[] { ',', '' });
this.textBoxCrew.Text = "Your crew:" + crew;
}
}
}
ItemHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace DamDarYar_IDMS
{
public class ItemHelper : DependencyObject
{
public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.RegisterAttached("IsChecked", typeof(bool?), typeof(ItemHelper), new PropertyMetadata(false, new PropertyChangedCallback(OnIsCheckedPropertyChanged)));
private static void OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is Family && ((bool?)e.NewValue).HasValue)
foreach (Person p in (d as Family).Members)
ItemHelper.SetIsChecked(p, (bool?)e.NewValue);
if (d is Person)
{
int checked = ((d as Person).GetValue(ItemHelper.ParentProperty) as Family).Members.Where(x => ItemHelper.GetIsChecked(x) == true).Count();
int unchecked = ((d as Person).GetValue(ItemHelper.ParentProperty) as Family).Members.Where(x => ItemHelper.GetIsChecked(x) == false).Count();
if (unchecked > 0 && checked > 0 )
{
ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, null);
return;
}
if (checked>0)
{
ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, true);
return;
}
ItemHelper.SetIsChecked((d as Person).GetValue(ItemHelper.ParentProperty) as DependencyObject, false);
}
}
public static void SetIsChecked(DependencyObject element, bool? IsChecked)
{
element.SetValue(ItemHelper.IsCheckedProperty, IsChecked);
}
public static bool? GetIsChecked(DependencyObject element)
{
return (bool?)element.GetValue(ItemHelper.IsCheckedProperty);
}
public static readonly DependencyProperty ParentProperty = DependencyProperty.RegisterAttached("Parent", typeof(ItemHelper));
public static void SetParent(DependencyObject element, object Parent)
{
element.SetValue(ItemHelper.ParentProperty, Parent);
}
public static object GetParent(DependencyObject element)
{
return (object)element.GetValue(ItemHelper.ParentProperty);
}
}
}
DataModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace DamDarYar_IDMS
{
public class Family:DependencyObject
{
public string Name {get;set;}
public List<Person> Members { get; set; }
}
public class Person : DependencyObject
{
public string Name { get; set; }
}
}
Invalid expression term '=' ItemHelper.cs 21
Identifier expected ItemHelper.cs 21
; expected ItemHelper.cs 21
{ expected ItemHelper.cs 21
Identifier expected ItemHelper.cs 22
; expected ItemHelper.cs 22
{ expected ItemHelper.cs 22
Invalid expression term '=' ItemHelper.cs 22
Syntax error, '(' expected ItemHelper.cs 23
Invalid expression term '>' ItemHelper.cs 23
Syntax error, '(' expected ItemHelper.cs 23
Invalid expression term '>' ItemHelper.cs 23
) expected ItemHelper.cs 23
) expected ItemHelper.cs 23
Syntax error, '(' expected ItemHelper.cs 28
Invalid expression term '>' ItemHelper.cs 28
) expected ItemHelper.cs 28
} expected ItemHelper.cs 35
} expected ItemHelper.cs 35
Empty character literal xaml.cs 46
No overload for method 'RegisterAttached' takes 2 arguments ItemHelper.cs 44
The name "Family" does not exist in the namespace "clr-namespace:". xaml 12 Build
The name "ItemHelper" does not exist in the namespace "clr-namespace:". xaml 13 Build
The name "ItemHelper" does not exist in the namespace "clr-namespace:". xaml 19 Build
The name "Person" does not exist in the namespace "clr-namespace:". xaml 27 Build
The name "ItemHelper" does not exist in the namespace "clr-namespace:". xaml 28 Build
The name "ItemHelper" does not exist in the namespace "clr-namespace:". xaml 34 Build