StyleTrigger Setter to an other Element - c#

I have the following xaml-Code:
<Border x:Name="border">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding MessageType}" Value="Error">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Rectangle>
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding MessageType}" Value="Error">
<Setter Property="Fill" Value="Icon.ico" />
</DataTrigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
</Border>
(MessageType is a Enum)
How can i combine this two StyleTrigger into One? Is it possible?

Not sure what your parent object is, containing the border. But i assume a DataTemplate, other objects should be work similar.
<DataTemplate>
<Border x:Name="border">
<Rectangle x:Name="rect"/>
<Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding MessageType}" Value="Error">
<Setter TargetName="border" Property="Background" Value="Red" />
<Setter TargetName="rect" Property="Fill" Value="Icon.ico" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Updated for comments:
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="myTemplate">
<!-- above template -->
</DataTemplate>
</UserControl.Resources>
<Grid>
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource myTemplate}"/>
</Grid>
</UserControl>

Related

XAML Binding Parent field to a Child value from ResourceDictionnary

I made a style to create a TextBox with a Placeholder.
<Style TargetType="TextBox" x:Key="PlaceholderTextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Background="White" BorderBrush="Black" BorderThickness="1">
<Grid>
<TextBox VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
x:Name="MessageBox"
Background="Transparent"
TextWrapping="Wrap"
BorderThickness="0"
Margin="2,0,0,0"
Text="{Binding Message, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<TextBlock IsHitTestVisible="False"
Text="{TemplateBinding Tag}"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Margin="4,0,0,0"
Foreground="Gray"
FontStyle="Italic">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text,
ElementName=MessageBox}" Value="">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
It works well, but when I want to access the data from my TextBox, I can't get it to retrieve the data.
<StackPanel Orientation="Horizontal">
<Label Content="Filtre : " />
<TextBox x:Name="TxtFiltre" Width="120" Height="16" Margin="4,0,4,0"
KeyUp="TxtFiltre_KeyUp" Style="{StaticResource PlaceholderTextBox}"
Tag="Search" />
</StackPanel>
I tried to bind the Text value from the TextBox and the TextBlock in style, but none worked.
In debug I saw that the value entered in my TextBox can be found on the Text element, but the source is ParentTemplate and not Default. I don't understand the difference and if it has something to do with the style and not being able to retrieve the data.
If you want to re template the textbox then you need to start with a viable textbox template.
I wasn't particularly careful with what follows so consider this an illustration rather than bullet proof cut and paste.
It uses the background brush for the watermark.
<Window.Resources>
<Style TargetType="{x:Type TextBox}" x:Key="WatermarkTextBoxStyle">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="KeyboardNavigation.TabNavigation"
Value="None" />
<Setter Property="FocusVisualStyle"
Value="{x:Null}" />
<Setter Property="MinWidth"
Value="120" />
<Setter Property="MinHeight"
Value="20" />
<Setter Property="AllowDrop"
Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBoxBase}">
<Border Name="Border"
CornerRadius="2"
Padding="2"
BorderThickness="1">
<Border.Background>
<VisualBrush Stretch="None"
AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontSize="12"
Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}"
Foreground="LightGray" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Opacity" Value="0"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="">
<Setter Property="Opacity" Value="1"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsFocused, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="True">
<Setter Property="Opacity" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</VisualBrush.Visual>
</VisualBrush>
</Border.Background>
<Border.BorderBrush>
<SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
</Border.BorderBrush>
<ScrollViewer Margin="0" x:Name="PART_ContentHost" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel>
<TextBox Style="{StaticResource WatermarkTextBoxStyle}"
Tag="Watermark Text"/>
A markup in place version might be clearer. There's not a huge amount to this. I set the opacity to zero if the textbox has focus ( so the user is about to start typing ) or the text is not "". Meaning they typed something in.
<TextBox>
<TextBox.Background>
<VisualBrush Stretch="None"
AlignmentX="Left">
<VisualBrush.Visual>
<TextBlock FontSize="12"
Text="Watermark Text"
Foreground="LightGray" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Opacity" Value="0"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="">
<Setter Property="Opacity" Value="1"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsFocused, RelativeSource={RelativeSource AncestorType=TextBox}}" Value="True">
<Setter Property="Opacity" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</VisualBrush.Visual>
</VisualBrush>
</TextBox.Background>
</TextBox>

Changing Foreground color of ComboBox CheckBox

I am trying to change the foreground color of ComboBoxItem, however it does not apply, what am I doing wrong? Also I'm trying to change the foreground color of hovers on ComboBoxItem which does not work as well.
Here is my xaml:
<ComboBox Foreground="Yellow" Name="txtDispatch" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="0,0,15,0" Grid.Column="2" Grid.Row="0" materialDesign:HintAssist.Hint="Select Dispatch" SelectionChanged="txtDispatch_SelectionChanged">
<ComboBox.ItemTemplate >
<DataTemplate >
<CheckBox Foreground="Yellow" Name="chkDispatch" Width="220" Checked="txtDispatch_Checked" Unchecked="txtDispatch_Checked" Content="{Binding Dispatch.id}" IsChecked="{Binding Check_Status}" CommandParameter="{Binding Dispatch.id}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox Foreground="Black">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ComboBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="White"/> <!--Optional-->
<Setter Property="Foreground" Value="Red"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="White"/> <!--Optional-->
<Setter Property="Foreground" Value="Yellow"/>
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
</ComboBox.Style>
<!-- Items List-->
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>5</ComboBoxItem>
</ComboBox>
Very Simple. Use the style.

MultiTrigger Condition to same binding/ Switch controls using Trigger

I am trying to have p:MarqueeTextBlock to only scroll on ListBox IsSelected under TargetType="{x:Type ListBoxItem}".
Having trouble to work out adding a MultiTrigger conditions (fired when IsSelected becomes True or False) to change the Property="Scroll" of the p:MarqueeTextBlock to Off or LeftTypewriter. In addition not sure how to reference the p:MarqueeTextBlock in the DataTemplate; perhaps if I provide it an x:Name? However I get Template property has already been set on DataTemplate.
I need to add AOTbMain_MARQUEE somehow in x:Key="ListBoxStyle_GAME" like:
<p:MarqueeTextBlock Text="{Binding Path=Title}" Style="{StaticResource AOTbMain_MARQUEE}"/>
Code with above reference not added:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:p="clr-namespace:Plugin.Controls;assembly=Plugin.v1"
mc:Ignorable="d"
d:DesignHeight="2160" d:DesignWidth="3840">
<UserControl.Resources>
<ResourceDictionary>
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Image x:Name="SelectedGameOverlay" Source="{}pack://siteoforigin:,,,/Images/Selected Game Overlay.png" RenderOptions.BitmapScalingMode="HighQuality" Visibility="Hidden" />
<ContentPresenter x:Name="SelectedGameTextColour" HorizontalAlignment="Left" />
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Opacity" Value="1" />
<Setter TargetName="SelectedGameOverlay" Property="Visibility" Value="Visible" />
<Setter TargetName="SelectedGameTextColour" Property="TextElement.Foreground" Value="#FFFFFF" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Opacity" Value="1" />
<Setter TargetName="SelectedGameOverlay" Property="Visibility" Value="Visible" />
<Setter TargetName="SelectedGameTextColour" Property="TextElement.Foreground" Value="#FFFFFF" />
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxStyle" TargetType="p:AutoscrollListBox">
<Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemStyle}" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
</Style>
<Style x:Key="ListBoxStyle_BASE" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyle}">
<Setter Property="ScrollMode" Value="Center" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource ListBoxItemStyle}">
<Setter Property="Padding" Value="1" />
</Style>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="AOTbMain_MARQUEE" TargetType="p:MarqueeTextBlock">
<Setter Property="Scroll" Value="LeftTypewriter" />
<Setter Property="ScrollDelayBeg" Value="0:0:5" />
<Setter Property="ScrollDelayEnd" Value="0:0:3" />
<Setter Property="ScrollSpeed" Value="0:0:5" />
</Style>
<Style x:Key="ListBoxStyle_BASE_INDXFADER" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyle_BASE}">
<Style.Triggers>
<DataTrigger Binding="{Binding IndexVisibility}" Value="Visible">
<Setter Property="Opacity" Value="0.1" />
</DataTrigger>
<DataTrigger Binding="{Binding IndexVisibility}" Value="Hidden">
<Setter Property="Opacity" Value="1" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="ListBoxStyle_BASETB" TargetType="p:AutoscaleTextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis" />
<Setter Property="TargetFontSize" Value="30" />
</Style>
<Style x:Key="ListBoxStyle_GAME" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyle_BASE_INDXFADER}">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<p:AutoscaleTextBlock Text="{Binding Path=Title}" Style="{StaticResource ListBoxStyle_BASETB}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxItemStyleSearch" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<ContentPresenter x:Name="SelectedSearchTextColour" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True" />
<Setter Property="Opacity" Value="1" />
<Setter TargetName="SelectedSearchTextColour" Property="TextElement.Foreground" Value="#FFFFFF" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxStyleSearch" TargetType="p:AutoscrollListBox">
<Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemStyleSearch}" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style x:Key="ListBoxStyle_INDX" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyleSearch}">
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource ListBoxItemStyleSearch}">
<Setter Property="Padding" Value="1" />
</Style>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Viewbox HorizontalAlignment="Left">
<p:AutoscrollListBox Name="Index" Style="{StaticResource ListBoxStyle_INDX}" Visibility="{Binding IndexVisibility}" />
</Viewbox>
<p:AutoscrollListBox Name="Items" Style="{StaticResource ListBoxStyle_GAME}" />
</Grid>
</UserControl>
The following Style will show the AutoscrollListBox as content of the DataTemplate. Once the ListBoxItem is selected it will display the MarqueeTextBlock instead. If unselected it will switch back to the AutoscrollListBox.
Solution 1
<Grid>
<AutoscrollListBox Name="Items" >
<AutoscrollListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Resources>
<AutoscaleTextBlock x:Key="AutoscaleTextBlock"
Text="{Binding Title}"
Style="{StaticResource ListBoxStyle_BASETB}"
x:Shared="False" />
<MarqueeTextBlock x:Key="MarqueeTextBlock"
Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"
x:Shared="False" />
</DataTemplate.Resources>
<ContentControl x:Name="ContentPresenter" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="False">
<Setter TargetName="ContentPresenter"
Property="Content"
Value="{StaticResource AutoscaleTextBlock}"/>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="True">
<Setter TargetName="ContentPresenter"
Property="Content"
Value="{StaticResource MarqueeTextBlock}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</AutoscrollListBox.ItemTemplate>
</AutoscrollListBox>
</Grid>
Solution 2
In case you are working with nested resource dictionaries and can't apply the x:Shared attribute you can swap the controls directly from the Trigger:
<Grid>
<AutoscrollListBox Name="Items" >
<AutoscrollListBox.ItemTemplate>
<DataTemplate>
<ContentControl x:Name="ContentPresenter" />
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="False">
<Setter TargetName="ContentPresenter"
Property="Content">
<Setter.Value>
<AutoscaleTextBlock Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}" />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="True">
<Setter TargetName="ContentPresenter"
Property="Content">
<Setter.Value>
<MarqueeTextBlock Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"/>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</AutoscrollListBox.ItemTemplate>
</AutoscrollListBox>
</Grid>
Solution 3
Or toggle the Visibility:
<Grid>
<AutoscrollListBox Name="Items" >
<AutoscrollListBox.ItemTemplate>
<DataTemplate>
<Grid>
<AutoscaleTextBlock x:Name="AutoscaleTextBlock"
Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"
Visibilty="Visible" />
<MarqueeTextBlock x:Name="MarqueeTextBlock"
Text="{Binding Title}"
Style="{StaticResource AOTbMain_MARQUEE}"
Visibilty="Collapsed" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
Value="True">
<Setter TargetName="AutoscaleTextBlock"
Property="Visibility"
Value="Collapsed" />
<Setter TargetName="MarqueeTextBlock"
Property="Visibility"
Value="Visible" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</AutoscrollListBox.ItemTemplate>
</AutoscrollListBox>
</Grid>

Dynamic cell control in one column in WPF DataGrid

I want to create WPF DataGrid with dynamic cell control in one column based on value in another column as below image.
XAML
<Window.Resources>
<Style x:Key="DataGridCellStyle1" TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Height="21.96">
<ComboBox x:Name="cbCondition1">
<ComboBoxItem Content="1"/>
<ComboBoxItem Content="2"/>
</ComboBox>
<TextBox x:Name="tbCondition2" Text="text"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding TypeColumn}" Value="ComboBox">
<Setter Value="Visible" TargetName="cbCondition1" Property="Visibility"/>
<Setter Value="Hidden" TargetName="tbCondition2" Property="Visibility"/>
</DataTrigger>
<DataTrigger Binding="{Binding TypeColumn}" Value="TextBox">
<Setter Value="Hidden" TargetName="cbCondition1" Property="Visibility"/>
<Setter Value="Visible" TargetName="tbCondition2" Property="Visibility"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid HorizontalAlignment="Left" Width="500" Grid.ColumnSpan="2" VerticalAlignment="Top" x:Name="GridWorkers"
ItemsSource="{Binding Workers}" Grid.Row="1" SelectedItem="{Binding SelectedWorker}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Control Type" Binding="{Binding TypeColumn}" Width="90" IsReadOnly="True"/>
<DataGridTemplateColumn Header="Dynamic Control" CellStyle="{StaticResource DataGridCellStyle1}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
I'm able to achieve this with xaml style, but looking for programmatic way to dynamically add controls in DataGrid cell i.e from C# code both (mvvm approach and code-behind approach) is preferred. Other possible way for solution also appreciated.
You could use a ContentControl with a Style that sets the Content property based on the value of the ControlType source property, e.g.:
<DataGrid ... AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Control Type" Binding="{Binding ControlType}" />
<DataGridTemplateColumn Header="Dynamic Control">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding ControlType}" Value="ComboBox">
<Setter Property="Content">
<Setter.Value>
<ComboBox />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding ControlType}" Value="TextBox">
<Setter Property="Content">
<Setter.Value>
<TextBox Text="text...." />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

Attached Behavior in Trigger Causes Build Error

I have a Style with a control template and I am having trouble getting it to compile. I am trying to trigger an attached behavior. If I put it in the control template triggers it works fine...but if I put it in the textbox triggers I get a build error that says:
Cannot find the static member 'SelectAllProperty' on the type
'TextBoxBehavior'
Here is my code:
<Style x:Key="RenamingTextBox" TargetType="{x:Type TextBox}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Grid>
<TextBlock x:Name="block" Visibility="Visible" Text="{TemplateBinding Text}" Margin="0"/>
<TextBox x:Name="box" Visibility="Collapsed" Text="{TemplateBinding Text}" Margin="0">
<TextBox.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.Setters>
<Setter TargetName="box" Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
<!-- This next line gives an error even though it is the same format as the one below -->
<Setter Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
</Trigger.Setters>
</Trigger>
</TextBox.Triggers>
</TextBox>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding IsRenaming}" Value="true">
<DataTrigger.Setters>
<Setter TargetName="block" Property="TextBox.Visibility" Value="Collapsed" />
<Setter TargetName="box" Property="TextBox.Visibility" Value="Visible" />
<!-- Uncommenting below works fine -->
<!--<Setter TargetName="box" Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>-->
</DataTrigger.Setters>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
Any ideas as to why one gives a build error and the other doesn't?
Nevermind, I needed to put the triggers for the textbox in a style instead:
<TextBox x:Name="box" Visibility="Collapsed" Text="{TemplateBinding Text}" Margin="0">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Setter Property="FocusManager.FocusedElement" Value="{Binding RelativeSource={RelativeSource Self}}"/>
<Setter Property="behaviors:TextBoxBehavior.SelectAll" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>

Categories