I am trying to access a control from within a template using the Template.FindName("Name",templatedParent) function. For some reason it is returning null.
I am using the XamlReader to load the below xaml.
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2;assembly=WpfApplication2"
xmlns:ice="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:igecac="clr-namespace:namespace1;assembly=assembly1"
Loaded="Window_Loaded"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type igecac:ApplicationContainer}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igecac:ApplicationContainer}">
<ControlTemplate.Resources>
<DataTemplate x:Key="selectedItemTemplate">
<TextBlock Padding="10,10,10,10" Text="{Binding Path=Id}" Background="{DynamicResource PanelBrush}" Width="100" Height="50">
<TextBlock.Foreground>
<SolidColorBrush Color="Black" ice:Freeze="True" />
</TextBlock.Foreground>
</TextBlock>
</DataTemplate>
<DataTemplate x:Key="ItemTemplate">
<TextBlock Padding="10,10,10,10" Text="{Binding Path=Id}" Background="{DynamicResource ObjectStrokeBrush}" Width="100" Height="50">
<TextBlock.Foreground>
<SolidColorBrush Color="Black" ice:Freeze="True" />
</TextBlock.Foreground>
</TextBlock>
</DataTemplate>
</ControlTemplate.Resources>
<Grid Background="{DynamicResource ObjectGradientBrush}">
<Grid.RowDefinitions />
<Grid.ColumnDefinitions>
<ColumnDefinition Name="grd1" Width="110" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Canvas Margin="0,-7,0,0" Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=HostableWidth}" Height="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=HostableHeight}">
<ContentPresenter Name="PART_HostArea" Content="{Binding Path=SelectedPanel.Document.Editor}" />
</Canvas>
<igecac:CustomListBox Name="PART_AssociatedPanels" ItemsSource="{Binding Path=AssociatedPanels}" SelectedItem="{Binding Path=SelectedPanel, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" HorizontalContentAlignment="Stretch" Background="{DynamicResource ObjectGradientBrush}">
<igecac:CustomListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" IsItemsHost="True" />
</ItemsPanelTemplate>
</igecac:CustomListBox.ItemsPanel>
<igecac:CustomListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ItemTemplate}" />
<Setter Property="Control.HorizontalContentAlignment" Value="Stretch" />
<Setter Property="FrameworkElement.Margin" Value="1,1,1,1" />
<Setter Property="Control.Padding" Value="2,2,2,2" />
<Setter Property="UIElement.Visibility" Value="Visible" />
<Style.Triggers>
<Trigger Property="Selector.IsSelected" Value="True">
<Setter Property="ContentControl.ContentTemplate" Value="{StaticResource selectedItemTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
</igecac:CustomListBox.ItemContainerStyle>
</igecac:CustomListBox>
<Grid Name="CloseButton" Margin="-6,-6,0,0" Grid.Column="1" Width="50" Height="40" HorizontalAlignment="Left" Visibility="Visible" VerticalAlignment="Top">
<Grid.RowDefinitions />
<Grid.ColumnDefinitions />
<Rectangle Fill="{DynamicResource ObjectStrokeBrush}" Width="40" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="Close" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<Grid Name="OpenButton" Margin="-5,-6,0,0" Grid.Column="0" Width="50" Height="40" HorizontalAlignment="Left" Visibility="Hidden" VerticalAlignment="Top">
<Grid.RowDefinitions />
<Grid.ColumnDefinitions />
<Rectangle Fill="{DynamicResource ObjectStrokeBrush}" Width="40" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="Open" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger SourceName="CloseButton" RoutedEvent="Mouse.MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<BeginStoryboard.Storyboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_AssociatedPanels" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="OpenButton" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CloseButton" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard.Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger SourceName="OpenButton" RoutedEvent="Mouse.MouseDown">
<EventTrigger.Actions>
<BeginStoryboard>
<BeginStoryboard.Storyboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PART_AssociatedPanels" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="OpenButton" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Hidden}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="CloseButton" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00" Value="{x:Static Visibility.Visible}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard.Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="igecac:ApplicationContainer.HostableWidth" Value="720" />
<Setter Property="igecac:ApplicationContainer.HostableHeight" Value="590" />
</Style>
</Window.Resources>
<Grid>
<igecac:ApplicationContainer x:Name="_ae51329a9692472099e1c0ed6b6b7937"
Tag="TemplatePart" Width="800" Height="600"
HostableWidth="500" HostableHeight="500">
</igecac:ApplicationContainer>
<igecac:DecimalKeypad/>
<igecac:HexKeypad/>
</Grid>
In the above xaml i have a custom control called ApplicationContainer. I have overridden the OnApplyTemplate() method in the code representing the class and i apply the above style to the control(haven't defined x:Key so it gets applied by default).
I see that on XamlReader.Load() the OnApplyTemplate() not called. That apart, if i try accessing the _ae51329a9692472099e1c0ed6b6b7937.Template i can see everything defined in the template(So, i feel the template is applied properly)
But when i do this.Template.FindName("PART_HostArea",this) in the control class OnApplyTemplate method(i call it explicitly) it returns me null.
Have you tried GetTemplateChild(ControlName)?
Related
Why the following event triggers of grid MouseEnter/MouseLeave not working. As a workaround, I used IsMouseOver.
There are no errors, binding errors, simply it does not work without any idea what is the reason.
Any Idea?
Just to pass validation
Here is the code
<ItemsControl.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<Storyboard x:Key="OnGrdMouseEnter">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Item">
<EasingColorKeyFrame KeyTime="0" Value="#2097B5"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TimeFrameItemText">
<EasingColorKeyFrame KeyTime="0" Value="White"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="OnGrdMouseLeave">
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="Item">
<EasingColorKeyFrame KeyTime="0" Value="Transparent"/>
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="TimeFrameItemText">
<EasingColorKeyFrame KeyTime="0" Value="Black"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</DataTemplate.Resources>
<StackPanel >
<!--MouseLeftButtonDown="MenuItem_MouseLeftButtonDown"
MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp"-->
<Grid Cursor="Hand" Name="Item" IsEnabled="{Binding IsEnabled}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<!--<RowDefinition Height="1"></RowDefinition>-->
</Grid.RowDefinitions>
<Grid.InputBindings>
<MouseBinding Command="{Binding ItemCommand}" MouseAction="LeftClick"/>
</Grid.InputBindings>
<TextBlock x:Name="TimeFrameItemText" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1"
Foreground="{StaticResource PopupForegroundBrush}" Height="22" Text="{Binding TimeFrameLabel}"
Width="{Binding ElementName=popup, Path=DataContext.OptionTextWidth,Converter={StaticResource DummyConverter}}">
<TextBlock>
</Grid>
<Rectangle Width="Auto" Height="1" Visibility="{Binding IsLast,Converter={StaticResource BoolVisibilityConverter},ConverterParameter=Inverse}" Fill="{StaticResource PopupSeparatorBrush}"/>
</StackPanel>
<DataTemplate.Triggers>
<!--<Trigger SourceName="Item" Property="IsMouseOver" Value="True">
<Setter TargetName="Item" Property="Background" Value="#2097B5"/>
<Setter TargetName="TimeFrameItemText" Property="Foreground" Value="White"/>
</Trigger>-->
<EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="Item">
<BeginStoryboard x:Name="OnGrdMouseEnter_BeginStoryboard" Storyboard="{StaticResource OnGrdMouseEnter}"/>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave" SourceName="Item">
<BeginStoryboard x:Name="OnGrdMouseLeave_BeginStoryboard" Storyboard="{StaticResource OnGrdMouseLeave}"/>
</EventTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</Popup>
To make MouseEnter and MouseLeave event working for grid, set the background color of the grid to Transparent.
I'm new to WPF,
I'm trying to style a TabControl and I'm having difficulty styling the actual TabItems:
I'm sure it's probably not that difficult, but I just can't figure it out so any help would be most appreciated!
Here's the XAML for the TabControl so far:
<Window x:Class="FunctionalFun.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:gc="clr-namespace:FunctionalFun.UI"
mc:Ignorable="d"
Title="mY Application" Height="800" Width="1080" ResizeMode="NoResize">
<StackPanel>
<Grid>
<TabControl Name="TabControl1" TabStripPlacement="Left" Margin="-6,46,-14,-453">
<TabItem>
<TabItem.Header>
<Image Height="35" Width="35" Source="check-form_logo3.png"/>
</TabItem.Header>
<TabItem.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</TabItem.Resources>
<Canvas>
</Canvas>
</TabItem>
<TabItem >
<TabItem.Header>
<Image Height="35" Width="35" Source="calendar-icon_logo1.png"/>
</TabItem.Header>
<TabItem.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</TabItem.Resources>
<Canvas>
</Canvas>
</TabItem>
<TabItem >
<TabItem.Header>
<Image Width="35" Height="35" Source="table_logo4.png"/>
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
</TabControl>
</Grid>
</StackPanel>
</Window>
One option is to copy the default style of tab item, then make your changes of its border corner radius, background, etc. then give it a key, then apply your style to your actual control, like so:
<StackPanel>
<Grid>
<Grid.Resources>
<Style x:Key="CustomTabItem" TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid x:Name="Root"
Background="AntiqueWhite">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected" />
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{DynamicResource ControlPressedColor}" />
</ColorAnimationUsingKeyFrames>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderThickness)"
Storyboard.TargetName="Border">
<EasingThicknessKeyFrame KeyTime="0"
Value="1,1,1,0" />
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver" />
<VisualState x:Name="Disabled">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Panel.Background).
(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{DynamicResource DisabledControlDarkColor}" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.BorderBrush).
(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0"
Value="{DynamicResource DisabledBorderLightColor}"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="Border"
Margin="0,0,-4,0"
BorderThickness="1,1,1,1"
CornerRadius="10">
<Border.BorderBrush>
<SolidColorBrush Color="Black" />
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="{DynamicResource ControlLightColor}"
Offset="0.0" />
<GradientStop Color="{DynamicResource ControlMediumColor}"
Offset="1.0" />
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Border.Background>
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"
RecognizesAccessKey="True"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Panel.ZIndex"
Value="100" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<TabControl Name="TabControl1" TabStripPlacement="Left" Margin="-6,46,-14,-453">
<TabItem Style="{StaticResource CustomTabItem}">
<TabItem.Header>
<TextBlock Text="A" />
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
<TabItem Style="{StaticResource CustomTabItem}">
<TabItem.Header>
<TextBlock Text="B" />
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
<TabItem Style="{StaticResource CustomTabItem}">
<TabItem.Header>
<TextBlock Text="C" />
</TabItem.Header>
<Canvas>
</Canvas>
</TabItem>
</TabControl>
</Grid>
</StackPanel>
I'm new to WPF and I would like to change to color of the text and icon of a button template. But it seems I can only change the opacity.
I guess I should apply the style to the children of the button but I don't know how.
Here is the template:
<Button x:Name="btnApp1" Width="56" Height="66" Margin="0,0,0,0" Style="{StaticResource AppButton}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<iconPacks:PackIconMaterial Kind="StarOutline" Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="#FFFFFFFF" />
<TextBlock x:Name="tButton" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="#FFFFFFFF" FontWeight="Bold">PVIE</TextBlock>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
And here is the style:
<Style x:Key="AppButton" TargetType="{x:Type Button}">
<Setter Property="Opacity" Value="0.25" />
<Setter Property="Foreground" Value="#FFFF9966" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0:0.3" />
<!--<ColorAnimation Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="Green" Duration="0:0:0:0.3" />-->
<ColorAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="Green" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.25" Duration="0:0:0:0.3" />
<!--<ColorAnimation Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="White" Duration="0:0:0:0.3" />-->
<ColorAnimationUsingKeyFrames Storyboard.Target="{Binding RelativeSource={RelativeSource TemplatedParent}}"
Storyboard.TargetProperty="(TextBox.Foreground).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="White" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
Thanks for your help.
Bind the Foreground property of the TextBlock and the Icon in the ControlTemplate using a {TemplateBinding}:
<Button x:Name="btnApp1" Width="56" Height="66" Margin="0,0,0,0" Style="{StaticResource AppButton}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid>
<iconPacks:PackIconMaterial Kind="StarOutline" Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center"
Foreground="{TemplateBinding Foreground}" />
<TextBlock x:Name="tButton" HorizontalAlignment="Center" VerticalAlignment="Bottom"
Foreground="{TemplateBinding Foreground}" FontWeight="Bold">PVIE</TextBlock>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
I have a datagrid where rows MAY have a status that has a color code or data that requires them to be bolded. I have been able to easily get the bolding to work. If it does have a color code, I want the entire row background to be displayed in that color. I have scoured several sites and tried many different ways to get the row color coded, but none of them seem to work. What am I missing?
Datagrid.xaml (style):
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="#FFFFFF" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
<Setter Property="ValidationErrorTemplate">
<Setter.Value>
<ControlTemplate>
<TextBlock Margin="2,0,0,0"
VerticalAlignment="Center"
Foreground="Red"
Text="!" />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border x:Name="DGR_Border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#AAFFFF" />
<GradientStop Offset="1" Color="#AAFFD4" />
</LinearGradientBrush>
</Border.Background>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFFF" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFFF" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal_AlternatingRow">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFFF" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#E6F7FF" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Normal_Selected">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#2e90d1" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#2980b9" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOver">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFFE6" />
</ColorAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="DGR_Border" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<EasingColorKeyFrame KeyTime="0" Value="#FFFF80" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1"
ItemsPanel="{TemplateBinding ItemsPanel}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<DataGridDetailsPresenter Grid.Row="1"
Grid.Column="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>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="border"
Background="Transparent"
BorderBrush="Transparent"
BorderThickness="1"
SnapsToDevicePixels="True">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Unfocused" />
<VisualState x:Name="Focused" />
</VisualStateGroup>
<VisualStateGroup x:Name="CurrentStates">
<VisualState x:Name="Regular" />
<VisualState x:Name="Current">
<Storyboard>
<ColorAnimationUsingKeyFrames Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
<EasingColorKeyFrame KeyTime="0" Value="Gray" />
</ColorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ListManager.xaml (form):
<DataGrid
SelectedItem="{Binding SelectedLead, Mode=TwoWay}"
ItemsSource="{Binding LeadListFiltered}"
x:Name="grdWorkLeadList"
AutoGenerateColumns="False"
Margin="-10,8,2,0"
IsReadOnly="True"
AlternationCount="2"
SelectionChanged="grdWorkLeadList_SelectionChanged"
LoadingRow="grdWorkLeadList_LoadingRow">
<DataGrid.Columns>
<DataGridTextColumn Header="Campaign" Binding="{Binding SetupCampaign.Name}" />
<DataGridTextColumn Header="Action Status" Binding="{Binding ActionStatus}" />
<DataGridTextColumn Header="Action Date" Binding="{Binding ActionDate,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTemplateColumn Header="Name" SortMemberPath="Lastname">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Title }" />
<Run Text=" " />
<Run Text="{Binding Firstname }" />
<Run Text=" " />
<Run Text="{Binding Lastname }" />
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Product" Binding="{Binding Product }" />
<DataGridTextColumn Header="Sailing" Binding="{Binding Sailing,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Stateroom" Binding="{Binding Stateroom }" />
<DataGridTextColumn Header="Booking Date" Binding="{Binding BookingDate,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Cancel Date" Binding="{Binding CancelDate,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Disposition" Binding="{Binding DispositionValue}" />
<DataGridTextColumn Header="Booking" Binding="{Binding Booking }" />
<DataGridTextColumn Header="Run Date" Binding="{Binding RunDate ,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Phone" Binding="{Binding Phone }" />
<DataGridTextColumn Header="Followup" Binding="{Binding ActionFollowup}" />
<DataGridTextColumn Header="ID" Binding="{Binding ID }" />
<DataGridTextColumn Header="Assigned Agent" Binding="{Binding AssignedAgent }" />
<DataGridTextColumn Header="Active" Binding="{Binding ActiveStatus }" />
<DataGridTextColumn Header="Disposition Date" Binding="{Binding DispositionDate ,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Rebook #" Binding="{Binding ReBookNumber }" />
<DataGridTextColumn Header="Close Date" Binding="{Binding CloseDate ,StringFormat=\{0:MM dd yyyy\}}" />
<DataGridTextColumn Header="Reason" Binding="{Binding ReasonDescription}" />
<DataGridTextColumn Header="Code" Binding="{Binding Code }" />
</DataGrid.Columns>
</DataGrid>
Code-behind (cs):
private void grdWorkLeadList_LoadingRow(object sender, DataGridRowEventArgs e)
{
Lead item = e.Row.Item as Lead;
string color;
if (item != null)
{
//handle status colors
if (item.CurrentDisposition != null)
using (var db = new DAOutboundCM_DEVEntities())
{
color = (from d in db.SetupDispositionLists where d.ID == item.CurrentDisposition select d.Color).Single();
e.Row.Background = new SolidColorBrush((Color) ColorConverter.ConvertFromString(color));
e.Row.UpdateLayout();
}
//handle priority bolding
if (item.SetupCampaign.Name.Contains("Quotes"))
if (item.Phone != null && item.Phone != "")
e.Row.FontWeight = FontWeights.Bold;
}
}
In WPF it's good practice to only set a property once, otherwise you can get unexpected behavior. My guess why the bolding works and the coloring doesn't is because although you've set both in code behind, you've set the Background color in the Style also.
Does the issue remain when you remove the setter in your Style?
<Setter Property="Background" Value="#FFFFFF"/>
EDIT
Looks like you're unable to assign Background colors in code behind, because you haven't implemented the Background property in your template.
prop="{TemplateBinding Background}"
Digging a little deeper it turns out you're also setting the background three times in your code.
The dummy line you allready removed
In code behind when loading the rows
The row background through templated VisualStates
To see if your code behind works, you can
replace
<Border x:Name="DGR_Border"
BorderBrush="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="#AAFFFF" />
<GradientStop Offset="1" Color="#AAFFD4" />
</LinearGradientBrush>
</Border.Background>
with
<Border x:Name="DGR_Border"
BorderBrush="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="True"
Background="{TemplateBinding Background}">
and comment out your
<!--<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>-->
in your DataGridRow ControlTemplate.
I want to change size of button from 70 till 90 when mouse is over:
<Style TargetType="Button"
x:Key="RadialButton">
<Setter Property="Width"
Value="70"></Setter>
<Setter Property="Height"
Value="85"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Resources>
<Storyboard x:Key="Storyboard1">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Width)"
Storyboard.TargetName="ExtEllipse">
<EasingDoubleKeyFrame KeyTime="0:0:1"
Value="90" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<Grid Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="70"></RowDefinition>
<RowDefinition Height="15"></RowDefinition>
</Grid.RowDefinitions>
<Ellipse Width="70"
Height="70"
Stroke="Gray"
Grid.Row="0"
Name="ExtEllipse"
Fill="{x:Null}" />
<Ellipse Width="50"
Height="50"
Stroke="Gray"
Grid.Row="0"></Ellipse>
<TextBlock Grid.Row="1"
FontSize="13"
FontWeight="Bold"
TextAlignment="Center"
Foreground="Green">
<ContentPresenter RecognizesAccessKey="True"
Content="{TemplateBinding Button.Content}" />
</TextBlock>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
using:
<Button Content="Button"
HorizontalAlignment="Left"
Margin="36,140,0,147"
Width="151"
Style="{DynamicResource RadialButton}" />
but it does not work. Nothing happened. Why and how to solve this problem?
That's because you have Storyboard, but you don't play it.
Try add trigger to play that storyboard. Something like this:
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Storyboard1}" />
</Trigger.EnterActions>
</Trigger>
Btw this is result of your animation:
You have to start your Storyboard. Your EventTrigger does nothing.
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Storyboard="{StaticResource Storyboard1}" />
</EventTrigger>