Wpf TreeView With CheckBox - c#

I need to add "Select All" to my wpf application ,
I've tried to do a simple binding but it doesn't work .
Can Someone Please advise , performing a simple binding
or other strait forward solution ?
Here's my treeview xaml :
<TreeView Name="CurrentTreeView" Margin="5" BorderBrush="Transparent" BorderThickness="0"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox
Focusable="False"
Checked="CheckedItemCurreuntSession"
Unchecked="UnCheckedItemCurreuntSession"
Name="treeChk"
Tag="{Binding Path=ChName}"
Style="{StaticResource CheckBoxStyle1}"
IsChecked="{Binding Path=IsChecked}"
VerticalAlignment="Center"/>
<TextBlock VerticalAlignment="Center" Text="{Binding}"
Margin="5"
TextOptions.TextFormattingMode="Display"
TextOptions.TextHintingMode="Auto"
/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>

<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"/>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox Focusable="False" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent} }">
<ContentPresenter/>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>

Related

How can I change background behind border (ListView.ItemTemplete->datatemplete) when I hover or click?

My problem is when I click or hover in ListViewItem which also show the silver background:
enter image description here
this is my code xaml:
<ListView
Margin="0,30,0,0"
Height="600"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Name="ListViewFC" ItemsSource="{Binding ListWords, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}" >
<ListView.ItemTemplate>
<DataTemplate>
<Border
Width="340"
x:Name="Border"
Height="80"
Background="Pink"
CornerRadius="15"
BorderThickness="1"
>
<Grid>
<TextBlock
VerticalAlignment="Center"
x:Name="txtContent"
Foreground="Black"
Text="{Binding Question1,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
TextWrapping="NoWrap"
Margin="30 0 0 0"
FontSize="15"
/>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListViewItem}}, Path=IsSelected}" Value="True">
<Setter TargetName="Border" Property="Background" Value="White" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I want that When I hover or click It don't show the silver background.
pls, help me .Thanks.
Add this inside your ListView:
<ListView.Resources>
<Style BasedOn="{StaticResource TextBlockStyle}" TargetType="{x:Type TextBlock}" />
<Style BasedOn="{StaticResource ListViewItemStyle}" TargetType="{x:Type ListViewItem}" />
</ListView.Resources>
Then add this outside of your ListView (this displays a background of gold on hover):
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Gold" />
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
The default ControlTemplate of the ListView contains a Border with Padding of 2X. Hence you have to change its template something like this
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>

ControlTemplate DataTrigger is not fired in ItemsControl ControlTemplate

I have a NavigationMenuControl with an ObservableCollection<HtNavigationMenuQuickLinkItem>. Everything is working fine, but the Style on my HtMenuIcon Control is not triggered. Where the Visibility is changed correctly. Can someone please give me a hint where I have a mistake? QuickLinkSymbol is a DependencyProperty of an Enum.
I also want to put the Visibility Behavior into the DataTrigger section.
Navigation Menu
<Style TargetType="Navigation:HtNavigationMenu">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenu">
<Grid>
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=QuickLinkItems}"/>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
QuickLinkItem
<Style TargetType="Navigation:HtNavigationMenuQuickLinkItem">
<Style.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis"/>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Navigation:HtNavigationMenuQuickLinkItem">
<Controls:MyButton Width="40" Height="40" Margin="10,10,10,0">
<Viewbox Margin="3">
<Controls:HtMenuIcon x:Name="icon" Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsQuicklink, Converter={StaticResource BoolToVis}}"/>
</Viewbox>
</Controls:MyButton >
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding QuickLinkSymbol, RelativeSource={RelativeSource TemplatedParent}}" Value="Home">
<Setter TargetName="icon" Property="Style" Value="{StaticResource Home}"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You need to reference Home as {x:Static EnumNAmeSpace:EnumType.Home}.
Oh, and if QuickLinkSymbol is a DepProp of HtNavigationMenuQuickLinkItem,
just use Trigger instead of DataTrigger.

Avoid filter textox in checkbox column in datagrid in wpf

I have one datagrid.The columns header name is binding from one xml file.
Now I need to add filter text box for header template from datagrid.
without textbox filter option the datagrid shown as per the below.
After adding filter text box the datagrid as shown as below
I don't want to show textbox in first column i.e checkbox column,how to do this?
The xaml code is attached below
<UserControl .....>
<UserControl.Resources>
<ResourceDictionary>
<DataGridTemplateColumn x:Key="CustomCheckBoxTemplate">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate >
<CheckBox Name="ColumnHearderCheckBox" IsThreeState="True" PreviewMouseLeftButtonDown="OnHeaderCheckBoxMouseButtonDown" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox PreviewMouseLeftButtonDown="OnCellCheckBoxPreviewMouseLeftButtonDown" IsChecked="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType=DataGridRow}, Path=IsSelected,Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn >
<ContextMenu x:Key="ColumnHeaderContextMenu"
FontSize="{Binding PlacementTarget.FontSize, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}" >
<EventSetter Event= "Click" Handler="OnContextMenuItemClicked" />
<Setter Property="IsCheckable" Value="True" />
<Setter Property="Header" Value="{Binding Header}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Visibility}" Value="Visible">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding Visibility}" Value="Collapsed">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
<DataTrigger Binding="{Binding Visibility}" Value="Hidden">
<Setter Property="IsChecked" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ContextMenu.ItemContainerStyle>
</ContextMenu>
</ResourceDictionary>
</UserControl.Resources>
<Grid Margin="5,0,0,0">
<Grid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Height" Value="30"/>
<Setter Property="Foreground" Value="WhiteSmoke"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}" >
<Border x:Name="Border"
Padding="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="LayoutGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=ActualHeight}" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtId" Width="100" />
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<DataTemplate x:Key="CustomTemplate">
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding Converter={StaticResource StringToImageConverter}}" Height="30" Width="30" Margin="30,0,0,0" HorizontalAlignment="Right"/>
</StackPanel>
</DataTemplate>
<ContextMenu x:Key="RowMenu" DataContext="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}">
<MenuItem Header="Delete" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItems, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}" >
</MenuItem>
<MenuItem Header="Edit" Command="{Binding EditCommand}"/>
</ContextMenu>
<Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="ContextMenu" Value="{StaticResource RowMenu}" />
</Style>
</Grid.Resources>
<DataGrid x:Name="dataGrid" IsReadOnly="True" ItemsSource="{Binding PatientStudyList}" SelectionMode="{Binding SelectionMode, Converter={StaticResource SelectionModeToStringConverter}}"
AlternationCount="2" GridLinesVisibility="Horizontal" AutoGenerateColumns="False" BorderThickness="1"
RowStyle="{StaticResource DefaultRowStyle}" HorizontalAlignment="Stretch">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Unloaded">
<i:InvokeCommandAction Command="{Binding DataGridUnloadedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
<Setter Property="ContextMenu" Value="{StaticResource ColumnHeaderContextMenu}" />
<Setter Property="Height" Value="50" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
</Grid>
</UserControl>
You are changing the DataGridColumnHeader.Template with a new ControlTemplate
The CustomCheckBoxTemplate is providing a DataGridTemplateColumn.HeaderTemplate as DataTemplate which targets the DataGridColumnHeader.ContentTemplate. However, your ControlTemplate doesn't use the DataGridColumnHeader.ContentTemplate internally, so the HeaderTemplate part of your CustomCheckBoxTemplate will be ignored.
If it works for you, just define the DataGridColumnHeader.ContentTemplate instead of the DataGridColumnHeader.Template in your style (will behave a little differently).
<Style TargetType="{x:Type DataGridColumnHeader}" BasedOn="{StaticResource {x:Type DataGridColumnHeader}}">
...
<!-- Do not define Property="Template" here -->
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<!-- Similar to your ControlTemplate, but the TemplateBinding parts need to be reworked -->
Otherwise use the DataGridColumnHeader.ContentTemplate inside your ControlTemplate.
<!-- Old -->
<TextBlock Text="{Binding Content, RelativeSource=
{RelativeSource Mode=TemplatedParent}}"/>
<!-- Replace by -->
<ContentPresenter
Content="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>
Didn't test all part, so there might still be errors in my suggestion.
Edit Actually, the last part about the ContentPresenter doesn't prevent the txtId textbox. Here is an idea where the ControlTemplate is using Triggers to change the visible parts based on the ContentTemplate property being set. Note I changed some minor details like the ColumnDefinition Width for my testing.
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}" >
<Border x:Name="Border"
Padding="2"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<Grid x:Name="LayoutGrid"
Visibility="Collapsed">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"/>
<TextBox x:Name="txtId" Width="100" />
</StackPanel>
</Grid>
<ContentPresenter
x:Name="TemplatedContent"
Visibility="Visible"
Content="{Binding Content, RelativeSource={RelativeSource Mode=TemplatedParent}}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ContentTemplate" Value="{x:Null}">
<Setter Property="Visibility" TargetName="TemplatedContent" Value="Collapsed"/>
<Setter Property="Visibility" TargetName="LayoutGrid" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Filtering a ComboBox with a GroupStyle

I have a Telerik ComboBox which has a GroupStyle applied to it. I want to have it so that when the items are filtered, the group items disappear if they have no children items, and it continues up the hierarchy.
So, this is the initial setup:
Current:
Desired:
For reference:
GroupItem Style
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Path=Name}">
<ItemsPresenter Margin="20,0,0,0" />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger
Binding="{Binding Path=Name}"
Value="{x:Null}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ItemsPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
RadComboBox
<telerik:RadComboBox
Grid.Column="1"
DisplayMemberPath="Name"
IsEditable="True"
IsFilteringEnabled="True"
ItemsSource="{Binding Path=Analyzers}"
KeyboardNavigation.TabNavigation="Local"
OpenDropDownOnFocus="True"
SelectedItem="{Binding Path=Analyzer, Mode=OneWayToSource}"
SelectedValue="{Binding Path=AnalyzerId, Converter={utilities:NullToZeroValueConverter}}"
SelectedValuePath="Id"
Style="{StaticResource ResourceKey=RadComboBoxStyle.CanDisable}"
TabIndex="2">
<telerik:RadComboBox.ItemContainerStyle>
<Style
BasedOn="{StaticResource ResourceKey=RadComboBoxItemStyle}"
TargetType="telerik:RadComboBoxItem">
<Setter Property="ToolTip" Value="{Binding Path=Description}" />
</Style>
</telerik:RadComboBox.ItemContainerStyle>
<telerik:RadComboBox.GroupStyle>
<GroupStyle />
</telerik:RadComboBox.GroupStyle>
</telerik:RadComboBox>
I found the following (related) example on the Telerik forums.
Example

wpf - how to use dataTemplate for treeViewItems

The following code throws an exception:
<TreeView
ItemsSource="{Binding TreeRootInstance}"
x:Name="Htree"
ItemTemplate="CellTemplate"
SelectedItemChanged="HTree_OnSelectedItemChanged"
KeyDown="HTree_KeyDown">
<TreeView.Resources>
<DataTemplate x:Key="CellTemplate">
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ToggleButton x:Name="Expander"
HorizontalAlignment="Right"
Focusable="False"
Grid.Column="1"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type vw:TreeViewItem}}}"
ClickMode="Press"/>
<TextBlock
Text="{Binding Name}"
Grid.Column="0" />
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=HasItems, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="False">
<Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<Style TargetType="{x:Type TreeViewItem}" x:Key="aa">
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="TabIndex" Value="0"/>
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
<Setter Property="ItemsSource" Value="{Binding Children}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<StackPanel>
<Border Name="Bd">
</Border>
<ItemsPresenter x:Name="ItemsHost" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TreeView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeView}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle" Value="{StaticResource aa}" />
</Style>
</TreeView.Resources>
</TreeView>
(I've omitted some of the code but the main hirerchy is still there...)
The exception is a xaml parse exception, on ItemsControl.ItemTemplate.
Could you please tell me how to make this code work?
Thanks,
Li
You can't use a plain string as a resource locator, you need to use either a StaticResource markup extension, i.e., ItemTempate="{StaticResource CellTemplate}"
The ItemTemplate line should read:
ItemTemplate="{StaticResource CellTemplate}"

Categories