Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to create a program . But I am running in a basic issue.

What I have tried:

xaml
C#
<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

C#
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
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    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

C#
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

C#
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
Posted
Updated 7-Dec-18 16:12pm
v4
Comments
BillWoodruff 5-Dec-18 20:55pm    
What does the task of copying files from one directory to another have to do with all this code involving 'Person and 'ItemHelper ?
Richard MacCutchan 6-Dec-18 5:03am    
Something on the right hand side of the expression at line 21 is not valid.
ZurdoDev 6-Dec-18 10:58am    
Fix the error.

int checked = ...

"checked" is a keyword in C#. You either need to change the name of your variable, or prefix it with @:
int @checked = ...

C# Keywords | Microsoft Docs[^]

You're also missing the property type from your second RegisterAttached[^] call:
public static readonly DependencyProperty ParentProperty = DependencyProperty.RegisterAttached("Parent", typeof(Family), typeof(ItemHelper));

You should also look to reduce the number of casts in your code, either by caching the results or by using pattern matching[^]. For example, if you're using a recent version of the C# compiler:
private static void OnIsCheckedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    switch (d)
    {
        case Family family when e.NewValue is bool newValue:
        {
            foreach (Person p in family.Members)
            {
                ItemHelper.SetIsChecked(p, newValue);
            }
            break;
        }
        case Person person when person.GetValue(ItemHelper.ParentProperty) is Family family:
        {
            bool anyChecked = family.Members.Any(x => ItemHelper.GetIsChecked(x) == true);
            bool anyUnchecked = family.Members.Any(x => ItemHelper.GetIsChecked(x) == false);
            if (anyChecked)
            {
                if (anyUnchecked)
                {
                    ItemHelper.SetIsChecked(family, null);
                }
                else
                {
                    ItemHelper.SetIsChecked(family, true);
                }
            }
            else
            {
                ItemHelper.SetIsChecked(family, false);
            }
            break;
        }
    }
}
 
Share this answer
 
Comments
Member 14006859 7-Dec-18 22:14pm    
Richard Deeming,Thanks for your answer.
Previous problems were solved, but now there's another problem, which is that checking the father of the children does not tick, that is, the check box does not work properly.
Richard Deeming,Thanks for your answer.
Previous problems were solved, but now there's another problem, which is that checking the father of the children does not tick, that is, the check box does not work properly.
 
Share this answer
 
Comments
Patrice T 7-Dec-18 23:05pm    
delete this!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900