Focus and Selection Styling listbox items with multiple possible templates - wpf

Good afternoon,
I have a listbox that consumes a list of items from a model, Addresses and RAddresses (inherited from Addresses)
Now, previously I have been creating my item templates in the ItemContainerStyle so that I have only my own focus visuals (i.e. no dotted line, no light grey rectangle, etc). This has worked fine in the past.
I now need two templates, one for each type (Address and RAddress). I have created these in the ListBox.resources as default templates (example below):
<DataTemplate DataType="{x:Type DAL:RAddress}">
I also have my listbox's ItemContainerStyle property pointing to the following:
<Style x:Key="AddressContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
</Style>
Previously when the Template was applied to the style, this kept the dotted line and grey rectangle visual from displaying. However, now they do so and the VisualState settings I have set up do not seem to get applied.
Whole DataTemplate example follows:
<DataTemplate DataType="{x:Type DAL:Address}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="#4C000000" CornerRadius="10" Padding="0" BorderBrush="#00FF0000" BorderThickness="1">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="#80000000"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="#00FF0000"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="#80000000"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Height="Auto" Width="Auto">
<CheckBox x:Name="checkBox" Content="CheckBox" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Style="{DynamicResource EditGemStyle}" HorizontalAlignment="Right" IsChecked="{Binding EditMode}"/>
<StackPanel Margin="0" Orientation="Vertical" d:LayoutOverrides="Height">
<Grid x:Name="DisplayGrid" Height="Auto" Width="Auto" d:LayoutOverrides="Width" VerticalAlignment="Stretch" Margin="7,3,11,3">
<StackPanel Orientation="Vertical" Width="Auto" d:LayoutOverrides="Width, Height" Margin="0,0,10,0">
<TextBlock x:Name="AddressFriendlyName" TextWrapping="Wrap" Text="{Binding AddressFriendlyName}" FontSize="18.667" Style="{DynamicResource Data}"/>
<TextBlock x:Name="Address1" TextWrapping="Wrap" Text="{Binding Address1}" d:LayoutOverrides="Width" FontSize="13.333" Style="{DynamicResource Data}"/>
<TextBlock x:Name="Address2" TextWrapping="Wrap" Text="{Binding Address2}" FontSize="13.333" Style="{DynamicResource Data}"/>
<TextBlock x:Name="Address3" TextWrapping="Wrap" Text="{Binding Address3}" FontSize="13.333" Style="{DynamicResource Data}"/>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="City" TextWrapping="Wrap" Text="{Binding City}" FontSize="13.333" Style="{DynamicResource Data}"/>
<TextBlock TextWrapping="Wrap" Text=", " FontSize="13.333" Style="{DynamicResource Data}"/>
<TextBlock x:Name="State" TextWrapping="Wrap" Text="{Binding State}" Margin="0,0,5,0" FontSize="13.333" Style="{DynamicResource Data}"/>
<TextBlock x:Name="ZIP" TextWrapping="Wrap" Text="{Binding ZIP}" FontSize="13.333" Style="{DynamicResource Data}"/>
</StackPanel>
<TextBlock x:Name="Country" TextWrapping="Wrap" Text="{Binding Country}" FontSize="13.333" Style="{DynamicResource Data}"/>
</StackPanel>
</Grid>
<Grid x:Name="EditGrid" Height="Auto" Margin="7,3,11,0" Visibility="Collapsed">
<StackPanel Orientation="Vertical" Width="Auto" Margin="0,0,10,0" d:LayoutOverrides="Width, Height">
<TextBox x:Name="AddressFriendlyName1" TextWrapping="Wrap" Text="{Binding AddressFriendlyName}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
<TextBox x:Name="Address4" TextWrapping="Wrap" Text="{Binding Address1}" d:LayoutOverrides="Width" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
<TextBox x:Name="Address5" TextWrapping="Wrap" Text="{Binding Address2}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
<TextBox x:Name="Address6" TextWrapping="Wrap" Text="{Binding Address3}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="City1" TextWrapping="Wrap" Text="{Binding City}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
<TextBlock TextWrapping="Wrap" Text=", " Foreground="White"/>
<TextBox x:Name="State1" TextWrapping="Wrap" Text="{Binding State}" Margin="0,2,5,2" Template="{DynamicResource SnazzyTextBoxTemplate}" Foreground="White"/>
<TextBox x:Name="ZIP1" TextWrapping="Wrap" Text="{Binding ZIP}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
</StackPanel>
<TextBox x:Name="Country1" TextWrapping="Wrap" Text="{Binding Country}" Template="{DynamicResource SnazzyTextBoxTemplate}" Margin="0,2" Foreground="White"/>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Address2}" Value="{x:Null}">
<Setter TargetName="Address2" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Address3}" Value="{x:Null}">
<Setter TargetName="Address3" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditMode}" Value="False">
<Setter Property="Visibility" TargetName="DisplayGrid" Value="Visible"/>
<Setter Property="Visibility" TargetName="EditGrid" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding EditMode}" Value="True">
<Setter Property="Visibility" TargetName="DisplayGrid" Value="Collapsed"/>
<Setter Property="Visibility" TargetName="EditGrid" Value="Visible"/>
</DataTrigger>
</DataTemplate.Triggers>
Previously I have tried using a StyleSelector to select between ItemContainerStyles that were created (one for each type). This caused me to encounter an error "cannot animate '(0).(1)' on an immutable object instance", so I started looking at my present setup.
How can I enforce my own Focus and Selection styling on two separate Item templates hosted in the same ListBox?
Any help will be greatly appreciated.

I think you need to move the specification of the storyboards to the ItemContainerStyle, rather than having them specified in the DataTemplates themselves. This is more DRY and will lead to less complication.
<Window.Resources>
<DataTemplate DataType="{x:Type DAL:RAddress}">
<TextBlock>RAddress!</TextBlock>
</DataTemplate>
<DataTemplate DataType="{x:Type DAL:Address}">
<TextBlock>Address!</TextBlock>
</DataTemplate>
<Style x:Key="IHateDottedBorders" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="0,0,0,0"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="#80000000"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="#00FF0000"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="#80000000"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="Bd">
<EasingColorKeyFrame KeyTime="0" Value="Red"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox ItemContainerStyle="{StaticResource IHateDottedBorders}" ItemsSource="{Binding CollectionOfAddresses}" />
</Grid>

Related

Binding the textbox width of a ComboBox to the width of its parent ComboBox in Window.Resources (WPF)

I designed a custom ComboBox.
I'd like to bind my ComboBox's textbox width to the width of its parent ComboBox.
For example, if the width of the ComboBox changes in Grid.Children, its TextBox should likewise change.
XAML:
Note: Some unnecessary blocks have been removed for readability and convenience.
Note: I'm aware that the PART EditableTextBox section needs to be edited, but I'm not sure how.
<Window.Resources>
<Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="MinWidth" Value="20" />
<Setter Property="MinHeight" Value="20" />
<Setter Property="Padding" Value="4,4,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox" Storyboard.TargetProperty="(TextElement.Foreground). (SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FF888888" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="EditStates">
<VisualState x:Name="Editable">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="PART_EditableTextBox">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ContentSite">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Hidden}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Uneditable" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ToggleButton x:Name="ToggleButton"
Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2"
Focusable="false"
ClickMode="Press"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter x:Name="ContentSite"
IsHitTestVisible="False"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
Margin="3,3,23,3"
VerticalAlignment="Stretch"
HorizontalAlignment="Left">
</ContentPresenter>
<TextBox x:Name="PART_EditableTextBox"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBoxTemplate}"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Margin="3,3,23,3"
Focusable="True"
Foreground="{TemplateBinding Foreground}"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}" />
<Popup x:Name="Popup"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide">
<Grid x:Name="DropDown"
SnapsToDevicePixels="True"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" BorderBrush="DarkGray" BorderThickness="1" Background="White">
</Border>
<ScrollViewer BorderBrush="DarkGray" Padding="3,3,3,11" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</Trigger>
<Trigger SourceName="Popup" Property="AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="CornerRadius" Value="9" />
<Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<Style TargetType="ScrollBar" >
<Setter Property="Stylus.IsPressAndHoldEnabled" Value="True"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ScrollBar}">
<Grid>
<Border CornerRadius="0,9,9,0" Background="{StaticResource ScrollBarNormalBackground}" Width="15" Margin="2,2,0,0" BorderBrush="DarkGray" BorderThickness="1"/>
<Track x:Name="PART_Track" IsDirectionReversed="True" Uid="PART_Track">
<Track.Thumb>
<Thumb Uid="Thumb_0">
<Thumb.Template>
<ControlTemplate>
<Border Background="LightGoldenrodYellow" Width="15" Height="12" Margin="2,2,0,0" CornerRadius="0,9,9,0" BorderBrush="DarkGray" Cursor="Hand" BorderThickness="1"/>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Track.Thumb>
</Track>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Style.Resources>
</Style>
<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBoxItem}">
<Border x:Name="Border" CornerRadius="9" SnapsToDevicePixels="true" Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFC5CBF9" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFDDDDDD" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.Children>
<ComboBox x:Name="Category_ComboBox" GotFocus="Category_ComboBox_GotFocus" LostFocus="Category_ComboBox_LostFocus" ContextMenu="{StaticResource CustomContextMenu}" IsEditable="True" BorderBrush="DarkGray" HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="267" Margin="21,138,0,0"/>
</Grid.Children>
</Grid>
Thanks
The text box PART_EditableTextBox is only visible when the ComboBox is in edit mode (which is when ComboBox.IsEdit is enabled).
To make it stretch simply set the TextBox.HorizontalAlignment property to HorizontalAlignment.Stretch:
<TextBox x:Name="PART_EditableTextBox"
HorizontalAlignment="Stretch" />
In case you meant to make the items in the selection box to stretch set ComboBox.HorizontalContentAlignment to HorizontalAlignment.Stretch:
<ComboBox HorizontalContentAlignment="Stretch" />

How to add WPF button to each WPF DataGrid column

I have a WPF Datagrid style in my project. Now I want to add some buttons to each row, on the other hand, I want to be set the specific style to the buttons.
<Style TargetType="DataGrid">
<Setter Property="Background" Value="{DynamicResource SecondaryRegionBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="GridLinesVisibility" Value="None"/>
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="EnableRowVirtualization" Value="True"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="CanUserReorderColumns" Value="False"/>
<Setter Property="CanUserResizeRows" Value="False"/>
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DataGridColumnHeaderStyle}"/>
<Setter Property="RowHeaderStyle" Value="{StaticResource DataGridRowHeaderStyle}"/>
<Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}"/>
<Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}"/>
<Setter Property="Padding" Value="6"/>
<Setter Property="ColumnWidth" Value="*"/>
<Setter Property="hc:DataGridAttach.TextColumnStyle" Value="{StaticResource DataGridTextColumnStyle}"/>
<Setter Property="hc:DataGridAttach.EditingTextColumnStyle" Value="{StaticResource TextBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.ComboBoxColumnStyle" Value="{StaticResource TextBlockComboBoxStyle}"/>
<Setter Property="hc:DataGridAttach.EditingComboBoxColumnStyle" Value="{StaticResource ComboBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.CheckBoxColumnStyle" Value="{StaticResource CheckBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.EditingCheckBoxColumnStyle" Value="{StaticResource CheckBoxBaseStyle}"/>
<Setter Property="hc:DataGridAttach.ApplyDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGrid">
<ControlTemplate.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_VerticalScrollBar">
<EasingDoubleKeyFrame KeyTime="0:0:.2" Value=".8"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_HorizontalScrollBar">
<EasingDoubleKeyFrame KeyTime="0:0:.2" Value=".8"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Storyboard2">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_VerticalScrollBar">
<EasingDoubleKeyFrame KeyTime="0:0:.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PART_HorizontalScrollBar">
<EasingDoubleKeyFrame KeyTime="0:0:.2" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="ScrollViewer">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Command="{x:Static DataGrid.SelectAllCommand}" Margin="0,0,0,6" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType=DataGrid}}" Width="{Binding CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType=DataGrid},Converter={StaticResource DoubleMinConverter}}" Style="{StaticResource ButtonIcon}" hc:IconElement.Geometry="{StaticResource AllGeometry}" Foreground="{DynamicResource PrimaryBrush}"/>
<DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.ColumnSpan="2" Grid.Row="0" Grid.Column="1" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType=DataGrid}}"/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="{TemplateBinding CanContentScroll}" Grid.ColumnSpan="3" Grid.Column="0" Grid.Row="1"/>
<ScrollBar Style="{StaticResource ScrollBarBaseStyle}" Opacity="0" x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="{TemplateBinding ScrollableHeight}" Orientation="Vertical" Grid.Row="1" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}"/>
<ScrollBar Style="{StaticResource ScrollBarBaseStyle}" Opacity="0" x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Orientation="Horizontal" VerticalAlignment="Bottom" Grid.Row="1" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"/>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard Storyboard="{StaticResource Storyboard2}"/>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
as you see there are some styles for Texts or ComboBox but there is no style for buttons.
How can I add buttons to each row and set the specific button style?
First part of your question
How to add a button for each row in a Wpf datagrid ?
That's how you add a button foreach row in a WPF DataGrid.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="BtnClicked">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
So, you create a DataGridTemplateColumn and inside that you define DataTemplate and Inside that you can set Button or any other UI Element you want to show for each row e.g a CheckBox.
Now, Coming to second part of your question.
How to add a particular style to it ?
Well you can define style for this button like any other button you do or you can assign it a Style resource or template. Something like below,
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="BtnClicked" Background="White" Foreground="Black" BorderThickness="2" BorderColor="Black">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Or assign a style resource.
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="BtnClicked" Style="MyBtnStyle">Details</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

How to bind the ProgressBar Value to Textbox in ResourceDictionary?

I have a custom ProgressBar control in XAML and I want to set a TextBox over it to write the ProgressBar Value. But I can't bind the Value correctly.
The code part to be fixed:
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="ValueChanged">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetName="txt"
Storyboard.TargetProperty="Text">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding Path=Value,RelativeSource={RelativeSource TemplatedParent}, StringFormat={}{0:0}%}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
UPDATED: Whole code in style.xaml (ResourceDictionary)
<Style x:Key="colorizedPB" TargetType="ProgressBar">
<Setter Property="Background" Value="Red"/>
<Setter Property="BorderBrush" Value="LightGray"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ProgressBar">
<Viewbox>
<Border HorizontalAlignment="Left" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
BorderBrush="Gray" BorderThickness="1">
<Grid>
<Rectangle x:Name="rect" HorizontalAlignment="Left" Fill="{TemplateBinding Background}" Width="0" Height="{TemplateBinding Height}">
<Rectangle.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</Rectangle.RenderTransform>
</Rectangle>
<TextBlock x:Name="txt" FontSize="13" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" Text="test"/>
</Grid>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="ValueChanged">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetName="txt"
Storyboard.TargetProperty="Text">
<DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ProgressBar}}, Path=Value, Mode=OneWay}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
In MainWindow.xaml
<Grid Grid.Column="1" Margin="8,102,10,53">
<ProgressBar Background="LightBlue" Name="progressBar" Style="{DynamicResource colorizedPB}" Width="200" Height="20"/>
</Grid>
Delete the EventTrigger and try this:
<TextBlock
x:Name="txt"
FontSize="13"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{TemplateBinding Value}"
/>
You may want to format Value, in which case you'll need to use a Binding instead:
<TextBlock
x:Name="txt"
FontSize="13"
Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, StringFormat={}{0:0.00}}"
/>

Want to change the Cell background color in wpf datagrid

I am binding Rows with Classes.
On each binding Row i want change the Cell back ground color on base of cell value.
<DataGrid x:Name="dtstandardview" BorderThickness="0" Height="429" Width="688" BorderBrush="Aqua" MouseLeftButtonDown="dtstandardview_MouseRightButtonUp_1"
GridLinesVisibility="None" MouseRightButtonUp="dtstandardview_MouseRightButtonUp_1"
VerticalScrollBarVisibility="Visible" AutoGenerateColumns="False" IsReadOnly="True"
CanUserDeleteRows="False" AlternationCount="2" CanUserResizeRows="False" Sorting="dtstandardview_Sorting_1"
Background="#DCDCDC" HeadersVisibility="Column" CanUserResizeColumns="False"
RowHeight="27" SelectionUnit="FullRow" CanUserAddRows="False" MinRowHeight="27" LoadingRow="dtstandardview_LoadingRow" LoadingRowDetails="dtstandardview_LoadingRowDetails" Loaded="dtstandardview_Loaded" Initialized="dtstandardview_Initialized" CellEditEnding="dtstandardview_CellEditEnding" AutoGeneratingColumn="dtstandardview_AutoGeneratingColumn" UnloadingRow="dtstandardview_UnloadingRow" UnloadingRowDetails="dtstandardview_UnloadingRowDetails" SelectionChanged="dtstandardview_SelectionChanged" >
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="Selected" Handler="dataGridRowSelected"/>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF0000"/>
</DataGrid.Resources>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="#cde0e5" />
<Setter Property="Background" Value="#cde0e5" />
</Trigger>
</Style.Triggers>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="#404040"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="DataContext">
<Setter.Value>
<TextBlock Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
</TextBlock.Effect>
</TextBlock>
</Setter.Value>
</Setter>
<Setter Property="Margin" Value="10,5" />
<Setter Property="VerticalContentAlignment" Value="Bottom"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="#404040"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="DataContext">
<Setter.Value>
<TextBlock Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="0" Color="#FF000000" Direction="-60" Opacity="0.32" ShadowDepth="1"/>
</TextBlock.Effect>
</TextBlock>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<!--<Setter Property="BorderBrush" Value="#c7dae0" />
<Setter Property="Background" Value="#c7dae0"></Setter>
<Setter Property="BorderThickness" Value="2" />-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<ControlTemplate.Resources>
<Storyboard x:Key="SelectedOn">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="select_gradient" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="SelectedOff">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="select_gradient" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HoverOn">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="hover_gradient" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.85" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="highlight" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="0.65" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="HoverOff">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="hover_gradient" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="highlight" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</SelectiveScrollingGrid.RowDefinitions>
<Rectangle x:Name="hover_gradient" StrokeThickness="1" RadiusX="1" RadiusY="1" Opacity="0" IsHitTestVisible="False" Grid.Column="1" Fill="{DynamicResource MouseOverBrush}"/>
<Rectangle x:Name="highlight" Margin="1" StrokeThickness="1" RadiusX="0.5" RadiusY="0.5" Opacity="0" IsHitTestVisible="False" Grid.Column="1" Stroke="{DynamicResource MouseOverHighlightBrush}" Fill="{DynamicResource MouseOverHighlightBrush}"/>
<Rectangle x:Name="select_gradient" Grid.Row="0" Grid.ColumnSpan="2" StrokeThickness="1" RadiusX="1" RadiusY="1" Opacity="0" IsHitTestVisible="False" Fill="{DynamicResource PressedBrush}" Stroke="{DynamicResource PressedBorderBrush}"/>
<DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
<DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
</SelectiveScrollingGrid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True" SourceName="DGR_Border">
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource HoverOff}" x:Name="HoverOff_BeginStoryboard" />
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource HoverOn}" x:Name="HoverOn_BeginStoryboard" />
</Trigger.EnterActions>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Trigger.ExitActions>
<BeginStoryboard x:Name="SelectedOff_BeginStoryboard" Storyboard="{StaticResource SelectedOff}" />
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource SelectedOn}" />
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.RowBackground >
<ImageBrush ImageSource="/ClientApplication;component/Images/second_row_bg.png"/>
</DataGrid.RowBackground>
<DataGrid.AlternatingRowBackground>
<ImageBrush ImageSource="/ClientApplication;component/Images/bonus_progress_bg.png"/>
</DataGrid.AlternatingRowBackground>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="ContentTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock Foreground="#404040" FontWeight="Bold" Margin="10,0,0,0" TextWrapping="Wrap" Text="{Binding}" TextOptions.TextFormattingMode="Display">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="0" Color="#FFFFFF" Direction="-90" Opacity="0.40" ShadowDepth="1" RenderOptions.ClearTypeHint="Auto" />
</TextBlock.Effect>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/ClientApplication;component/Images/table_bg_header.png"/>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>
<ImageBrush ImageSource="/ClientApplication;component/Images/titel_bg.png"/>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#404040" />
<Setter Property="BorderThickness" Value="0, 0, 1, 0"/>
<Setter Property="Height" Value="26" />
<Setter Property="FontSize" Value="14"/>
<Setter Property="FontFamily" Value="Arial"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn Width="125" Header="Table" Binding="{Binding Path=Table}">
</DataGridTextColumn>
<DataGridTextColumn Width="101" Header="Stakes" Binding="{Binding Path=Stakes}" />
<DataGridTextColumn Width="95" Header="Game" Binding="{Binding Path=Game}" />
<DataGridTemplateColumn Header="Type" Width="86">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate x:Name="typedatatempl">
<StackPanel Orientation="Horizontal" Name="stackpaneltype">
<TextBlock Name="typedate" Text="{Binding Path=Type}" Margin="2,0,0,2" />
<TextBlock Name="txtblocknumber" Text="{Binding Path=number}" Margin="2, 0, 0, 2" Background="Gray" TextAlignment="Center" FontFamily="Arial" FontWeight="Bold" FontSize="10" Foreground="White" Height="13" Width="13" VerticalAlignment="Center" HorizontalAlignment="Center" />
<TextBlock Name="txtblockalpha" Text="{Binding Path=alphabate}" Margin="5,0,0,2" Background="Transparent" FontFamily="Arial" TextAlignment="Center" FontWeight="Bold" FontSize="10" Foreground="White" Height="13" Width="13" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Width="83" Header="Players" Binding="{Binding Path=Players}" />
<DataGridTextColumn Width="117" Header="Average Pot" Binding="{Binding Path=Average}" />
<DataGridTextColumn Width="63" Header="H/Hr" Binding="{Binding Path=hhr}" />
</DataGrid.Columns>
</DataGrid>
How can i change the back color of cell base on value.
I did on dtstandardview_LoadingRow event but not working .
In your Triggers collection on the DataGridCell Style add the following
<DataGrid ItemsSource="{Binding Items}">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Value}" Value="true">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
And this would be your view model and entity class
public class MyViewModel
{
public MainWindow()
{
DataContext = this;
Items =
new ObservableCollection<MyEntity>
{
new MyEntity(true),
new MyEntity(false),
new MyEntity(true)
};
}
public ObservableCollection<MyEntity> Items { get; set; }
}
public class MyEntity
{
public MyEntity(bool value)
{
Value = value;
}
public bool Value { get; set; }
}

Focus on Label, TextBlock and Border

I want to create a flat button with rounded right top and bottom corners. This button needs to have the background changed on clicked and on mouse over.
Currently my Markup looks like this:
<Border x:Name="MyButton" Height="25" Margin="0,5,0,0" CornerRadius="0 5 5 0" BorderThickness="1" BorderBrush="Gray" Style="{StaticResource myStyle1}">
<StackPanel Orientation="Horizontal" Margin="8,0,0,0">
<Image Source="image.jpg" Height="20"/>
<TextBlock Text="My Button"/> <!-- Could also be a label if needs to be. -->
</StackPanel>
</Border>
<Style x:Key="myStyle1" TargetType="{x:Type Border}">
<Setter Property="Background" Value="{StaticResource MainContentForegroundColor}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
The mouse over trigger works perfectly but i can't get the click trigger to work... i've tried IsKeyboardFocusWithin and IsFocused but it didn't work.
I think you should use ToggleButton instead of normal button -
<ToggleButton
Background="Transparent">
<ToggleButton.Template>
<ControlTemplate
TargetType="{x:Type ToggleButton}">
<Border
x:Name="MyButton"
Height="25"
Margin="0,5,0,0"
CornerRadius="0 5 5 0"
BorderThickness="1"
BorderBrush="Gray">
<StackPanel
Orientation="Horizontal"
Margin="8,0,0,0">
<Image
Source="image.jpg"
Height="20" />
<TextBlock
Text="My Button" />
<!-- Could also be a label if needs to be. -->
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger
Property="IsChecked"
Value="True">
<Setter
Property="Background"
Value="Red" />
</Trigger>
<Trigger
Property="IsMouseOver"
Value="True">
<Setter
Property="Background"
Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ToggleButton.Template>
</ToggleButton>
You can create a Button style, then only you will IsPressed Property. See the below code using VSM.
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF760D0D"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
<SplineColorKeyFrame KeyTime="00:00:00" Value="#FF675A88"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="border" BorderBrush="#FF5A8876" BorderThickness="3" Background="#FFF4EDED"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button HorizontalAlignment="Left" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" Width="180" Height="61" Content="Button"/>

Resources