Binding Selected TreeNode MVVM - c#

all night racking my head - I can not figure out how to bind a selected segment TreeView variable Mvvm
<TreeView ItemsSource="{Binding Subunits}">
<!-- Шаблон подразделения -->
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Positions}">
<TextBlock Text="{Binding SubunitName}"/>
<!-- Шаблон должности -->
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Employees}" DataType="{x:Type local:Employee}">
<TextBlock Text="{Binding PositionName}"/>
<!-- Шаблон сотрудника -->
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0" MouseDown="qwe"/>
<TextBlock Text="{Binding FirstName}" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Patronymic}" Margin="0,0,5,0"/>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
How can i do it?
sorry for my english :(

Related

Using ReactiveUI to bind to WPF TreeView

I am trying to bind to a TreeView with ReactiveUI in WPF, but nothing is being displayed. Can anyone tell me where I am going wrong, or give me some advice on how to debug it?
Binding in View constructor:
this.OneWayBind(ViewModel,
vm => vm.NonTableItemCommentsViewModels,
view => view.CommentTree.ItemsSource);
Xaml:
<TreeView Name="CommentTree" ItemsSource="{Binding}">
<TreeView.Resources>
<!-- Template for Items -->
<HierarchicalDataTemplate DataType="{x:Type viewModels:NonTableItemCommentsViewModel}"
ItemsSource="{Binding Path=Comments, Mode=OneWay}">
<TextBlock Text="{Binding Path=ItemId, Mode=OneWay}" />
</HierarchicalDataTemplate>
<!-- Template for Comments -->
<DataTemplate DataType="{x:Type viewModels:CommentViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Text, Mode=OneWay}"
Margin="3,0" />
<TextBlock Text="{Binding Path=Author, Mode=OneWay}"
Margin="3,0" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Items and Comments are both ObservableCollections.

How to apply multiple styles in WPF TreeView

I am beginner on WPF and need help.
Problem:
I have 2 Tree View on the form and need to apply 2 different style on pair of 2 Tree View Comparison.
Is there any way we can achieve this ?
I have 2 Tree View look like this
<TreeView x:Name="Source1PermissionTreeView" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="630" Width="890" >
<TreeView.Resources>
<DataTemplate DataType="{x:Type entities:SharePointUser}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\user.png"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</DataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SitePermission}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\sitepermission.png"/>
<TextBlock Text="{Binding Path=Title}"></TextBlock>
</DockPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SharePointGroup}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\group.png"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SharePointList}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\List.PNG"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type entities:SharePointSite}">
<DockPanel>
<CheckBox />
<Image Source="E:\SWorking\SharePointSecurityApps\SharePointSecurityApps\SharePointSecurityApps.WPF\Images\Site.png"/>
<TextBlock Text="{Binding Path=Title}"/>
</DockPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
How can I apply style in tree view?

Listview Checkbox.IsEnabled bind to Parent Checkbox.IsChecked

I try to bind the IsEnabled Property of a Checkbox to IsChecked of another Checkbox in a Listview. What is wrong?
<TreeView ItemsSource="{Binding Rulesets}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Rules}">
<StackPanel Orientation="Horizontal">
<CheckBox Name="rulesetCheckbox" IsChecked="{Binding IsActivated}"></CheckBox>
<TextBlock Text="{Binding Name}" />
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsEnabled="{Binding ElementName=rulesetCheckbox, Path=IsChecked}" IsChecked="{Binding IsActivated}"></CheckBox>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Thanks!

ContextMenu only on specific TreeViewItem

Is there any possible solution to show custom ContextMenu on specific TreeViewItem?
My TreeView looks like that:
<TreeView Name="AliasTree" ItemsSource="{Binding aliases}">
<TreeView.Resources >
<HierarchicalDataTemplate DataType="{x:Type models:ConnectionModel}" ItemsSource="{Binding schemas}">
<TextBlock Text="{Binding alias}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:SchemaModel}" ItemsSource="{Binding schema_collections}">
<TextBlock Text="{Binding SCHEMA_NAME}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type models:SchemaCollection}" ItemsSource="{Binding collection}">
<TextBlock Text="{Binding SCHEMA_COLLECTION_NAME}" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type models:TableModel}" >
<TextBlock Text="{Binding TABLE_NAME}" />
</DataTemplate>
</TreeView.Resources>
</TreeView>
I want to show ContextMenu only on TreeViewItem based on "TableModel".
Later I will make custom ContextMenu for other elements.
How can I do that?
Thank You for any help,
Wiktor

Binding is not working with data template in data template

I have a ListView with an nested ItemTemplate for presenting orders.
Each order is presented within a Expander. These Expanders have a ContentTemplate for presenting all positions within each order. And these order positions are also in a Expander.
The ListView gets its data from an ObservableCollection (AvailableOrders) which contains all orders. These order object have a ObservableCollection "Items" holding all positions for this order.
But I'm not able to get the bindings work properly. How should I properly set the binding for the "inner expander" to show information about the items?
All ideas are appreciated!
<ListView ItemsSource="{Binding VMOrder.AvailableOrders}">
<ListView.ItemTemplate>
<DataTemplate>
<Expander Content="{Binding}">
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Order " />
<TextBlock Text="{Binding Id}" />
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.ContentTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.Template>
<ControlTemplate>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Material.Name}" />
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.ContentTemplate>
<DataTemplate>
<TextBlock Text="TEST" />
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I've figured it out now.
I need to use a relative source in the data templates and set the content property of each expander.
<ListView ItemsSource="{Binding VMOrder.AvailableOrders}">
<ListView.ItemTemplate>
<DataTemplate>
<Expander Content="{Binding}">
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Order " />
<TextBlock Text="{Binding DataContext.Id, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" />
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.ContentTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Content="{Binding}">
<Expander.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.Material.Name, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" />
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding DataContext.Material.Description, RelativeSource={RelativeSource FindAncestor, AncestorType=Expander}}" />
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
For the inner ItemsControl, you have defined the control template for the whole control. You have to define the ItemTemplate instead
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Material.Name}" />
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.ContentTemplate>
<DataTemplate>
<TextBlock Text="TEST" />
</DataTemplate>
</Expander.ContentTemplate>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

Categories

Resources