Is there a way to use IsMouseOver in a resource dictionary - c#

When I use the below block of code in a Window or control it works (I can see the button animation and the size of the button changes when I hover over it), but it does not work when I use it in the resource dictionary. I am creating a Prism app with mah apps window.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:b="http://schemas.microsoft.com/expression/2010/interactivity"
>
<!--// Merge Dic here //-->
<ContentControl x:Key="OperatorUIPreview"
Content="{StaticResource splashStyle}"/>
<ContentControl x:Key="OperatorUI.Home"
Background="Purple">
<Grid >
<Border
Background="#323345"
CornerRadius="10,10,0,0"
Margin="0,85,0,0">
<Grid >
<Button
x:Name="home1"
Background="Blue"
Style="{StaticResource DockBtnStyle}">
<Popup Width="70"
HorizontalOffset="-12"
PlacementTarget="{Binding ElementName=home1}"
IsOpen="{Binding ElementName=home1, Path=IsMouseOver, Mode=OneWay}"
Style="{StaticResource PopupStyle}">
<Grid>
<Border Style="{StaticResource border}">
<TextBlock Text="Home"
Style="{StaticResource PopupText}" />
</Border>
<Path Style="{StaticResource ArrowPath}"/>
</Grid>
</Popup>
</Button>
</Grid>
</Border>
</Grid>
</ContentControl>
</ResourceDictionary>
Style:
<Style x:Key="DockBtnStyle"
TargetType="Button">
<Setter Property="Height"
Value="50" />
<Setter Property="Width"
Value="50" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderThickness="0"
Background="{TemplateBinding Background}" />
</ControlTemplate>
</Setter.Value>
</Setter>
<!--// Button Animation //-->
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Button.Width)"
To="65"
Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetProperty="(Button.Height)"
To="65"
Duration="0:0:0.3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Button.Width)"
To="50"
Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetProperty="(Button.Height)"
To="50"
Duration="0:0:0.3" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>

Related

ToggleButton: Style doesn't work with IsChecked binding

I have a ToggleButton defined like this:
<ToggleButton x:Name="tbtn_delivery_advice" IsChecked="{Binding ServiceOptions.Advice.Requested}" Style="{DynamicResource ToggleButtonStyle}" Grid.Column="0" Grid.Row="1" Width="28" Margin="2 7" VerticalAlignment="Top"/>
and this style, that I found on the internet:
<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Viewbox>
<Border x:Name="Border" CornerRadius="10"
Background="#FFFFFFFF"
Width="40" Height="20"
BorderBrush="#FF707070"
BorderThickness="1">
<Ellipse x:Name="Ellipse" Fill="#FFFFFFFF" Stretch="Uniform"
Margin="2 1 2 1"
Stroke="LightGray" StrokeThickness="1"
HorizontalAlignment="Stretch">
</Ellipse>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Checked">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#FF4CD661"
Duration="0:0:0.1" />
<ThicknessAnimation Storyboard.TargetName="Ellipse"
Storyboard.TargetProperty="Margin"
To="20 1 2 1"
Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Unchecked">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="White"
Duration="0:0:0.1" />
<ThicknessAnimation Storyboard.TargetName="Ellipse"
Storyboard.TargetProperty="Margin"
To="2 1 2 1"
Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The Button works just fine and looks like expected when IsChecked is not bound to anything or to a property with an initial value of false. But as soon as the initial value is true I get an exception. The message states that the name Border can't be found in System.Windows.Controls.ControlTemplate.
I have no idea how to solve this problem and would appreciate any help.
I found a solution even thought I'm still not a hundred percent sure what the problem was.
I thought it may be, that the Checked event trigger gets called before the button is fully initialized. So I simply switched from EventTrigger to Trigger and now everything works fine.
This is my working style:
<Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Viewbox>
<Border x:Name="Border" CornerRadius="10"
Background="#FFFFFFFF"
Width="40" Height="20"
BorderBrush="#FF707070"
BorderThickness="1">
<Ellipse x:Name="Ellipse" Fill="#FFFFFFFF" Stretch="Uniform"
Margin="2 1 2 1"
Stroke="LightGray" StrokeThickness="1"
HorizontalAlignment="Stretch">
</Ellipse>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="#FF4CD661"
Duration="0:0:0.1" />
<ThicknessAnimation Storyboard.TargetName="Ellipse"
Storyboard.TargetProperty="Margin"
To="20 1 2 1"
Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="White"
Duration="0:0:0.1" />
<ThicknessAnimation Storyboard.TargetName="Ellipse"
Storyboard.TargetProperty="Margin"
To="2 1 2 1"
Duration="0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

WPF: button control template not firing click event

I am making my own costumed button, which has a label in it. For some reason, my button doesn't react to the 'click' event, and the click function doesn't lunch. why?
here is my button style xaml code:
<Style x:Key="MyMenuButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#AF4EB4EC"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Margin" Value="0,0,5,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid >
<Rectangle Fill="{TemplateBinding Background}" StrokeThickness="0" MinWidth="{Binding ElementName=lblCnt,Path=Width}"/>
<Label Name="lblCnt" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" Background="{TemplateBinding Background}"
Height="auto" Width="auto"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Background" Value="White"/>
</Trigger>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard >
<Storyboard>
<!--<DoubleAnimation Storyboard.TargetProperty="Width"
Duration="0:0:0.200" By="30"/>-->
<DoubleAnimation Storyboard.TargetProperty="FontSize"
Duration="0:0:0.200"
From="12" To="22"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
Duration="0:0:0.200"
From="#AF4EB4EC" To="White"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
Duration="0:0:0.200"
From="White" To="#AF4EB4EC"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard >
<!--<DoubleAnimation Storyboard.TargetProperty="Width"
Duration="0:0:0.100"
By="-30"/>-->
<DoubleAnimation Storyboard.TargetProperty="FontSize"
Duration="0:0:0.100"
From="22" To="12"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
Duration="0:0:0.100"
From="White"
To="#AF4EB4EC"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
Duration="0:0:0.100"
From="#AF4EB4EC"
To="White"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
and this is the complete xaml code:
<Window x:Class="FMS_Csharp_GUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="File Indexer" Height="552" Width="864" Icon="magna-folder-icon.ico">
<Window.CommandBindings>
<CommandBinding Command="New" CanExecute="CanExeNewRepo" Executed="ClickNewRepo"/>
<CommandBinding Command="Delete" CanExecute="CanExeNewRepo" Executed="ClickDeleteRepo"/>
</Window.CommandBindings>
<Window.Resources>
<StackPanel x:Key="documentStackPanelStyle" Orientation="Horizontal" >
<Image Source="Images/openDoc.ico" Width="32" Height="32"/>
<Label Content="" VerticalAlignment="Center"/>
</StackPanel>
<Style x:Key="MyMenuButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#AF4EB4EC"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Margin" Value="0,0,5,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid >
<Rectangle Fill="{TemplateBinding Background}" StrokeThickness="0" MinWidth="{Binding ElementName=lblCnt,Path=Width}"/>
<Label Name="lblCnt" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" Background="{TemplateBinding Background}"
Height="auto" Width="auto"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Background" Value="White"/>
</Trigger>
<EventTrigger RoutedEvent="UIElement.MouseEnter">
<BeginStoryboard >
<Storyboard>
<!--<DoubleAnimation Storyboard.TargetProperty="Width"
Duration="0:0:0.200" By="30"/>-->
<DoubleAnimation Storyboard.TargetProperty="FontSize"
Duration="0:0:0.200"
From="12" To="22"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
Duration="0:0:0.200"
From="#AF4EB4EC" To="White"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
Duration="0:0:0.200"
From="White" To="#AF4EB4EC"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave">
<BeginStoryboard>
<Storyboard >
<!--<DoubleAnimation Storyboard.TargetProperty="Width"
Duration="0:0:0.100"
By="-30"/>-->
<DoubleAnimation Storyboard.TargetProperty="FontSize"
Duration="0:0:0.100"
From="22" To="12"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
Duration="0:0:0.100"
From="White"
To="#AF4EB4EC"/>
<ColorAnimation Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
Duration="0:0:0.100"
From="#AF4EB4EC"
To="White"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid x:Name="grdContents" Margin="0,23,0,76">
<Grid Name="grdUserInfo" HorizontalAlignment="Right" Height="50" VerticalAlignment="Top" Width="189" Margin="0,-18,0,0">
<Image Name="imgUser" Source="Images/notloggedin.jpg" HorizontalAlignment="Right" Height="49.667" VerticalAlignment="Top"/>
<Label Name="lblUser" Content="you are not logged in!" Margin="0,0,54.667,0" HorizontalAlignment="Right" Width="134.333" Height="49.667" VerticalAlignment="Top"/>
</Grid>
<StackPanel Name="stkpMyStyledMenu" Height="47" Margin="0,10,0,0" Background="#FF4EA2EC" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left">
<StackPanel.Resources>
<Style BasedOn="{StaticResource MyMenuButtonStyle}" TargetType="Button"/>
</StackPanel.Resources>
<Button Content="New Repo" Click="myMenuNewRepo"/>
<Button Content="Open Repo" Click="MyMenuClickOpenRepo" />
<Button Content="Login" Click="MyMenuClickLogin"/>
<Button Content="Exit" Click="MyMenuClickExit"/>
</StackPanel>
<GroupBox Header="Input" Name="grpInput" HorizontalAlignment="Left" Margin="145,0,0,120" VerticalAlignment="Bottom" RenderTransformOrigin="0.352,-0.304" Height="185" Width="290">
</GroupBox>
<Button Content="Button" HorizontalAlignment="Left" Margin="205,70,0,0" VerticalAlignment="Top" Width="75" Click="MyMenuClickNewRepo"/>
<Button Content="Button" Click="MyMenuClickOpenRepo" HorizontalAlignment="Left" Margin="285,70,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
<ListBox Name="lstMessages" Height="61" VerticalAlignment="Bottom"/>
</Grid>
Use Snoop's events tab to determine which control is handling your click.
Could it be you set something in a way that the label gets the click? You could check binding some handler-code to the Click event of the label.

How to write a custom textbox in WPF with other components "glued" on it?

I have been for 2 days now trying to develop my own WPF textbox for no avail.
My initial try was inheriting from a control and then adding a template with all the stuff I wanted (a border and an asterisk in the outside).
Generic.xaml file:
<Style TargetType="{x:Type local:BdlTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:BdlTextBox">
<Grid x:Name="ContentGrid">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="8"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" x:Name="Tbl" Text="*" Foreground="Red" FontSize="15" Margin="0,-4,0,0"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:BdlTextBox}},
Path=IsRequired, Converter={StaticResource myBoolToVisibilityConverter}}"/>
<Rectangle Grid.Column="1" x:Name="Bg" Fill="Red" Opacity="0"/>
<TextBox Grid.Column="1" x:Name="Tb" Margin="1,1,1,1"/>
</Grid>
</Grid>
<ControlTemplate.Resources>
<Storyboard x:Key="flashAnimation">
<DoubleAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="Opacity" From="0" To="1" AutoReverse="True" Duration="0:0:0.5" RepeatBehavior="2x" />
<DoubleAnimation Storyboard.TargetName="Bg" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.5" BeginTime="0:0:2"/>
</Storyboard>
</ControlTemplate.Resources>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=HasValidationError}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard Name="flash" Storyboard="{StaticResource flashAnimation}" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<StopStoryboard BeginStoryboardName="flash"/>
</DataTrigger.ExitActions>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=HasValidationError}" Value="False" >
<Setter TargetName="Bg" Property="Opacity" Value="0"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The problem is: I cannot use the control as a Textbox (the reason is obvious). I can't even properly use the Text property. So I was wondering what could be done to solve this problem.
Dependency Properties are also a problem. I have tried to "clone" the Text property from the Textbox "Tb", but failed too.

Animate button background color in XAML

I new to WPF and XAML, so I have ResourceDictionary (one button for now):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ButtonProduct" TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border"
CornerRadius="0"
BorderThickness="0"
Focusable="False"
BorderBrush="Transparent" Background="White">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="#52b0ca"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
On hover the color of the button changes, but how can I make change in fade in and out, for smooth transition of the color?
You can use EventTrigger to start ColorAnimation on MouseEnter and MouseLeave:
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="0" BorderThickness="0" Focusable="False" BorderBrush="Transparent" Background="White">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="White" To="#52b0ca" Duration="0:0:1" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="#52b0ca" To="White" Duration="0:0:1" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>

WPF User Control Resources

I have a custom control extending the WPF Datagrid. Inside Custom control, the resources and control template of the grid are defined
<DataGrid.Resources>
<local:VisibilityConverter x:Key="visibilityConverter" />
<Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
<Setter Property="Width" Value="8" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Cursor" Value="SizeWE" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Thumb}">
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--May need Commented if clashes with other Styles-->
<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DataGridColumnHeaderStyle">
<Setter Property="Foreground" Value="#FF000000" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="IsTabStop" Value="False" />
<Setter Property="SeparatorBrush" Value="#FFC9CACA" />
<Setter Property="Padding" Value="4" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridColumnHeader">
<Grid x:Name="Root" Height="48">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="26" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" Duration="0" To="#FF448DCA" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[3].Color" Duration="0" To="#7FFFFFFF" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[2].Color" Duration="0" To="#CCFFFFFF" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[1].Color" Duration="0" To="#F2FFFFFF" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" Duration="0" To="#FF448DCA" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[0].Color" Duration="0" To="#D8FFFFFF" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[1].Color" Duration="0" To="#C6FFFFFF" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[2].Color" Duration="0" To="#8CFFFFFF" />
<ColorAnimation Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Fill).(GradientStops)[3].Color" Duration="0" To="#3FFFFFFF" />
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SortStates">
<VisualState x:Name="Unsorted" />
<VisualState x:Name="SortAscending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
</Storyboard>
</VisualState>
<VisualState x:Name="SortDescending">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
<DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="(RenderTransform).ScaleY" Duration="0" To="-.9" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="BackgroundRectangle" Grid.ColumnSpan="2" Fill="#FF1F3B53" Stretch="Fill" />
<Rectangle x:Name="BackgroundGradient" Grid.ColumnSpan="2" Stretch="Fill">
<Rectangle.Fill>
<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
<GradientStop Color="#FCFFFFFF" Offset="0.015" />
<GradientStop Color="#F7FFFFFF" Offset="0.375" />
<GradientStop Color="#E5FFFFFF" Offset="0.6" />
<GradientStop Color="#D1FFFFFF" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Grid Grid.ColumnSpan="2" Margin="0,0,1,1">
<Border Margin="0,0,0,-1" Width="14" BorderBrush="#FF878787" BorderThickness="1,0,0,0" HorizontalAlignment="Right" Visibility="Collapsed">
<Path x:Name="DropDownIndicator" Height="5.26" Margin="0,2,0,0" Width="6.334" Data="F1 M-3.0081699E-07,0.010437 L4.0493598,6.9732699 L8.0546999,0 L-3.0081699E-07,0.010437 z" Fill="#FF444444" HorizontalAlignment="Center" RenderTransformOrigin=".5,.5" Stretch="Uniform" UseLayoutRounding="False" VerticalAlignment="Center">
<Path.RenderTransform>
<ScaleTransform ScaleX=".9" ScaleY=".9" />
</Path.RenderTransform>
</Path>
</Border>
</Grid>
<Grid Margin="4,2,9,2" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<local:ColumnOptionControl Height="13" Width="13" />
<!--Visibility="{Binding Path=CanUserGroup, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={StaticResource visibilityConverter}}" />-->
<ContentPresenter Grid.Column="1" Content="{TemplateBinding Content}" />
<Path x:Name="SortIcon" Grid.Column="2" Margin="4,0,0,0" Width="8" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z " Fill="#FF444444" HorizontalAlignment="Right" Opacity="0" RenderTransformOrigin=".5,.5" Stretch="Uniform" VerticalAlignment="Center">
<Path.RenderTransform>
<ScaleTransform ScaleX=".9" ScaleY=".9" />
</Path.RenderTransform>
</Path>
</Grid>
<Rectangle x:Name="VerticalSeparator" Grid.Column="1" Width="1" Fill="{TemplateBinding SeparatorBrush}" VerticalAlignment="Stretch" /> <!--Visibility="{TemplateBinding SeparatorVisibility}" />-->
<Border Grid.ColumnSpan="2" Grid.Row="1" Margin="0" BorderBrush="#FFC9CACA" BorderThickness="0,1,1,0" Visibility="{Binding Path=CanUserFilter, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={StaticResource visibilityConverter}}">
<local:ColumnFilterControl Margin="0,0,0,1"/> <!--FilterVisibility="{Binding Path=CanUserFilter, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Converter={StaticResource visibilityConverter}}" />-->
</Border>
<Thumb Style="{StaticResource ColumnHeaderGripperStyle}" x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" />
<Thumb Style="{StaticResource ColumnHeaderGripperStyle}" x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
And the VisibilityConverter defined as
[ValueConversion(typeof(Visibility), typeof(bool))]
class VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? val = value as bool?;
if (val == null) val = false;
Visibility visible = (val == false ? Visibility.Collapsed : Visibility.Visible);
return visible;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility? visible = value as Visibility?;
if (visible == null) visible = Visibility.Collapsed;
bool val = (visible == Visibility.Collapsed || visible == Visibility.Hidden ? false : true);
return val;
}
}
In the usage of the control, when I attempt to declare a resources like below, run time exception is thrown at the below declaration as
'Set property 'System.Windows.ResourceDictionary.DeferrableContent' threw an exception.' Line number '368' and line position '30'.
with the inner exception
{"Cannot re-initialize ResourceDictionary instance."}
<local:EnhancedWPFDataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
</Style>
<uc:Settings x:Key="settings"/>
</local:EnhancedWPFDataGrid.Resources>
If I comment above Resources from the xaml, it works fine.
Could you please shed some lights what is wrong with this?
Thanks in advance.
I think you're trying to redefine the ResourceDictionary that you've created when setting up your custom control. You've already created the resource dictionary for use with the control, and you're then attempting to create it again (hence the re-initialize exception).
You could move your styling for the DataGridCell into the main template (which you wouldn't want to do in your case, because of your EvenSetter, which is presumably instance specific).
A better solution for you may be to define that style in another ResourceDictionary and reference it, or add the Style to the resources for the Window/Page where you're using the custom control (e.g. you could add it to the resources of a parent Grid).
I believe you can also merge resource dictionaries, although I've never tried it:
http://msdn.microsoft.com/en-us/library/aa350178.aspx

Categories