How to apply multiple styles in WPF TreeView - c#

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?

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.

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

ItemTemplate definition in another UserControl

I have the following usercontrol to define my general control-layout:
<UserControl>
<DockPanel LastChildFill="False">
<ListView DockPanel.Dock="Left"
ItemsSource="{Binding FoundResults}"
SelectedItem="{Binding SelectedItem}"
ItemTemplate="{DynamicResource FoundResultsStyle}"/>
<ContentControl DockPanel.Dock="Top"
Name="WinSock"
Content="{Binding ElementName=BaseWindowUserControl, Path=SpecificView}" />
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="{Binding StatusBarText, Mode=OneWay}" />
</StatusBar>
</DockPanel>
</UserControl>
Every window should have a ListView on the left side as you can see.
I need a way to define the ItemTemplate in the concrete userControl.
Person-Usercontrol:
<UserControl.Resources>
<DataTemplate x:Key="FoundResultsStyle" DataType="{x:Type Pocos:Person}">
<StackPanel>
<TextBlock Text="{Binding Lastname}"/>
<TextBlock Text="{Binding Firstname}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
The ListView doesn't use my Template which I have defined in my 'concrete' usercontrol. Is there a way to achieve this?
Thank you in advance!
Can you do something like this, so that the DataType, as it changes, will select the correct template?
<UserControl>
<UserControl.Resources>
<DataTemplate DataType="{x:Type Pocos:Person}">
...
</DataTemplate>
<DataTemplate DataType"{x:Type Pocos:Dog}">
...
</DataTemplate>
</UserControl.Resources>
<DockPanel LastChildFill="False">
<ListView
DockPanel.Dock="Left"
ItemsSource="{Binding FoundResults}"
SelectedItem="{Binding SelectedItem}"/>
<ContentControl
DockPanel.Dock="Top"
Name="WinSock"
Content="{Binding ElementName=BaseWindowUserControl, Path=SpecificView}" />
<StatusBar DockPanel.Dock="Bottom">
<StatusBarItem Content="{Binding StatusBarText, Mode=OneWay}" />
</StatusBar>
</DockPanel>
</UserControl>

Binding Selected TreeNode MVVM

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 :(

Categories

Resources