PepeGak
PepeGak
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
i'll try it. Thanks!
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
it seems to fit my problem
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
Hmm
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
I don't quite understand what does this command do
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
I'm pretty sure that it violates the MVVM pattern but I hope my teachers accept that
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
and in my viewmodel I'm working with Tree_View
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
and XAML code for TreeView tag is
<TreeView Grid.Column="0" Grid.Row="1" x:Name="tree1" ItemsSource="{Binding GRZ_and_OBL}"
TreeViewItem.Selected="OnTreeItemSelected">
<TreeView Grid.Column="0" Grid.Row="1" x:Name="tree1" ItemsSource="{Binding GRZ_and_OBL}"
TreeViewItem.Selected="OnTreeItemSelected">
So when the item is selected, treeview is copied in my MainWindowViewModel
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
Best thing I could do is:
private void OnTreeItemSelected(object sender, RoutedEventArgs e)
{
if (sender is TreeView trVw)
{
this.MWVM.Tree_View = trVw;

if (trVw.SelectedItem is SubjectModel SM)
this.MWVM.SelectedSubject = SM;
else if (trVw.SelectedItem is TreeItemViewModel)
this.MWVM.SelectedSubject = null;
}
}
private void OnTreeItemSelected(object sender, RoutedEventArgs e)
{
if (sender is TreeView trVw)
{
this.MWVM.Tree_View = trVw;

if (trVw.SelectedItem is SubjectModel SM)
this.MWVM.SelectedSubject = SM;
else if (trVw.SelectedItem is TreeItemViewModel)
this.MWVM.SelectedSubject = null;
}
}
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
Just iterate through all OC's until the subject itself is found?
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
But there is now another problem. Let's say I choose a subject (Astana city for example). How do I access it using TreeView?
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}

private Action<object> execute;
private Func<object, bool> canExecute;

public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}

public bool CanExecute(object parameter)
=> this.canExecute == null || this.canExecute(parameter);
public void Execute(object parameter)
=> this.execute(parameter);
}
public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}

private Action<object> execute;
private Func<object, bool> canExecute;

public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}

public bool CanExecute(object parameter)
=> this.canExecute == null || this.canExecute(parameter);
public void Execute(object parameter)
=> this.execute(parameter);
}
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
and redid commands thing. I found RelayCommand implementation and it works fine for me
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
No description
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
Hi. I've tried the next thing: I created an ObservableCollection (let's call it OC) of TreeItemViewModel (TIVM) and inside the TIVM class there is OC<SubjectModel> of actual Subjects and string SubjectType {get;set;}
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
What is the propriate way to write a command for that? So when I click on a button (or any other thing that supports command implementation), object from that collection is taken and shown on the window (somewhere on Grid.Column="1")
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
Cities here is ObservableCollection<Subject>
38 replies
CC#
Created by PepeGak on 12/14/2023 in #help
✅ How to handle commands in TreeView?
I tried this
<TreeView Grid.Column="0">
<TreeViewItem Header="Cities" ItemsSource="{Binding Cities}">
<TreeViewItem.ItemTemplate>
<HierarchicalDataTemplate>
<Button Content="{Binding SubjectName}"
Command="{Binding ShowSubject}"/>
</HierarchicalDataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
<!--Same for oblasts, I guess-->
</TreeView>
<TreeView Grid.Column="0">
<TreeViewItem Header="Cities" ItemsSource="{Binding Cities}">
<TreeViewItem.ItemTemplate>
<HierarchicalDataTemplate>
<Button Content="{Binding SubjectName}"
Command="{Binding ShowSubject}"/>
</HierarchicalDataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
<!--Same for oblasts, I guess-->
</TreeView>
but it doesn't seem to work
38 replies