reusing user controls in silverlight - wpf

i'm relatively new to wpf/mvvm so this is probably something basic, but I have a user control that displays perfectly on its own, but when I try to load it into a stackpanel of a different user control, it won't display at all. does anyone have any idea of what i'm doing incorrectly here? i've tried setting up the width/height/minwidth/minheight, but none of it makes a difference.
here is the xaml for the re-usable user control:
<UserControl x:Class="BankProductControlLibrary.Views.IdentificationData"
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:bpcl="clr-namespace:BankProductControlLibrary.UserControls"
mc:Ignorable="d"
d:DesignHeight="102" d:DesignWidth="535">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/BankProductSilverlightControl;component/Resources/Style.xaml"/>
<ResourceDictionary>
<Style x:Key="textBlockStyle" TargetType="TextBlock">
<Setter Property="Height" Value="28"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="409" />
</Style>
<Style x:Key="textBlockStyle2" TargetType="TextBlock">
<Setter Property="Height" Value="23"/>
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="Auto" />
<Setter Property="Margin" Value="0,0,0,0" />
</Style>
<Style x:Key="comboBoxStyle" TargetType="ComboBox">
<Setter Property="Height" Value="23"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="180" />
<Setter Property="Margin" Value="0,0,0,0" />
</Style>
<Style x:Key="textBoxStyle" TargetType="TextBox">
<Setter Property="Height" Value="23"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="0,0,0,0" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="132" />
</Style>
<Style x:Key="dateBoxStyle" TargetType="bpcl:DateBox">
<Setter Property="Height" Value="23"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="0,0,0,0" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Width" Value="75" />
</Style>
<Style x:Key="dateFormatingExplanationStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="9"/>
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="Height" Value="23" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="75"/>
<Setter Property="Text" Value="(mm/dd/yyyy)" />
</Style>
<Style x:Key="placeOfIssuanceExplanationStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="9"/>
<Setter Property="FontStyle" Value="Normal" />
<Setter Property="Height" Value="23" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="Auto"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<StackPanel MinHeight="102" MinWidth="535">
<TextBlock Style="{StaticResource textBlockStyle}">
Select the ID provided from the drop-down list. ID must be a photo ID.
</TextBlock>
<Grid Style="{StaticResource ScreenStyle}" x:Name="LayoutRoot" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="110*" />
<ColumnDefinition Width="180" />
<ColumnDefinition Width="88*" />
<ColumnDefinition Width="142*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!--Column0-->
<TextBlock Style="{StaticResource textBlockStyle2}"
Grid.Row="0" Grid.Column="0"
Text="Type:"
/>
<TextBlock Style="{StaticResource textBlockStyle2}"
Grid.Row="1" Grid.Column="0"
Text="Date of Issuance:"
/>
<TextBlock Style="{StaticResource textBlockStyle2}"
Grid.Row="2" Grid.Column="0"
Text="Place of Issuance:" />
<!--Column1-->
<ComboBox Name="Ids" Style="{StaticResource comboBoxStyle}"
Grid.Row="0" Grid.Column="1"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding SelectedTaxpayerId, Mode=TwoWay}"
/>
<StackPanel Orientation="Horizontal"
Grid.Row="1" Grid.Column="1">
<bpcl:DateBox x:Name="IssueDate" Style="{StaticResource dateBoxStyle}"/>
<TextBlock Style="{StaticResource dateFormatingExplanationStyle}" />
</StackPanel>
<StackPanel Orientation="Horizontal"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2">
<!--This should be bpcl:AlphaTextBox-->
<TextBox Name="PlaceOfIssuance" Style="{StaticResource textBoxStyle}"
MaxLength="2"
Width="40"/>
<TextBlock Style="{StaticResource placeOfIssuanceExplanationStyle}"
Text="Enter state abbreviation or country code"
/>
</StackPanel>
<!--Column2-->
<TextBlock Style="{StaticResource textBlockStyle2}"
Grid.Row="0" Grid.Column="2"
Text="ID Number:"
/>
<TextBlock Style="{StaticResource textBlockStyle2}"
Grid.Row="1" Grid.Column="2"
Text="Expiration Date:"/>
<!--Column3-->
<TextBox Name="IdentificationNumber" Style="{StaticResource textBoxStyle}"
Grid.Row="0" Grid.Column="3" />
<StackPanel Orientation="Horizontal"
Grid.Row="1" Grid.Column="3" >
<bpcl:DateBox x:Name="ExpirationDate" Style="{StaticResource dateBoxStyle}"/>
<TextBlock Style="{StaticResource dateFormatingExplanationStyle}" />
</StackPanel>
</Grid>
</StackPanel>
</UserControl>
and here is where i try to consume it:
<UserControl x:Class="BankProductControlLibrary.Views.JTH.TaxpayerIdentificationView"
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:ltsa="http://www.libtax.com/LibTax/BankApp"
xmlns:bpcl="clr-namespace:BankProductControlLibrary.UserControls"
xmlns:views="clr-namespace:BankProductControlLibrary.Views"
mc:Ignorable="d ltsa"
d:DesignHeight="102" d:DesignWidth="520">
<UserControl.Resources>
<Style x:Key="validationStyle" TargetType="TextBlock">
<Setter Property="Height" Value="23" />
<Setter Property="Width" Value="Auto"/>
<Setter Property="Foreground" Value="Red" />
</Style>
</UserControl.Resources>
<StackPanel>
<views:IdentificationData x:Name="TPIdentification" />
<TextBlock Name="ValidationErrorMessage" Style="{StaticResource validationStyle}" />
</StackPanel>
</UserControl>

I think you should check if the user control has datacontext. There is a nice tool that helped me a lot with WPF:
http://wpftutorial.net/Inspector.html

Related

WPF XAML: Button's background color doesn't change on mouseover [duplicate]

This question already has answers here:
Change color of Button when Mouse is over
(5 answers)
Closed 1 year ago.
I need to change text color, cursor and background of a button on mouse hover. I'm trying to do this by changing styles but also tried adding style trigger directly to the button
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</Style.Triggers>
Text color and cursor change, but background color remain the same (light blue). Please tell me what could be the problem?
Screenshot
Full code:
<Window x:Class="WpfApp3.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:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
Title="Заголовок" Height="640" Width="960">
<Window.Resources>
<Style TargetType="Button" x:Key="SidebarButton">
<Style.Setters>
<Setter Property="Control.FontFamily" Value="Segoe UI" />
<Setter Property="Control.Background" Value="#333742" />
<Setter Property="Control.Foreground" Value="White" />
<Setter Property="Control.BorderThickness" Value="0" />
<Setter Property="Control.Padding" Value="10" />
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid Background="#333742">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*" MinWidth="150" MaxWidth="200"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Orientation="Horizontal" Grid.Column="0" Grid.Row="0" Background="#414550"></StackPanel>
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="0"></StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="0" Grid.Row="1" Background="#333742">
<Border Height="80">
<TextBlock FontSize="18" Foreground="#fff" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="30">Меню</TextBlock>
</Border>
<Button Style="{StaticResource SidebarButton}">Печать кодов</Button>
<Button Style="{StaticResource SidebarButton}">Настройки</Button>
<Button Style="{StaticResource SidebarButton}">О программе</Button>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Column="1" Grid.Row="1" Background="#2d3039"></StackPanel>
</Grid>
</Window>
You can find the answer in this response.
You can define the style like this :
<Style TargetType="Button" x:Key="SidebarButton">
<Style.Setters>
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="Background" Value="#333742" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" Padding="10">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Cursor" Value="Hand" />
</Trigger>
</Style.Triggers>
</Style>

XAML Warning "The resource could not be resolved"

I am using DynamicResources in WPF, I have made a button style in app.xaml. Below is the definition:
<Application.Resources>
<Style x:Key="InteractionButtonStyle"
TargetType="{x:Type Button}">
<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="5" />
</Style>
<SolidColorBrush x:Key="StyleBasedForegroundColor" Color="White"/>
<SolidColorBrush x:Key="StyleBasedBackgroundColor" Color="Black" />
</Style.Resources>
<Setter Property="Background" Value="{StaticResource StyleBasedBackgroundColor}" />
<Setter Property="Foreground" Value="{StaticResource StyleBasedForegroundColor}" />
<Setter Property="FontSize" Value="15" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Padding" Value="10" />
</Style>
</Application.Resources>
Now when I use this style in a page:
<Button Grid.Row="3"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Click="RegisterNew_Clicked"
Style="{StaticResource InteractionButtonStyle}"
Visibility="{Binding ShowRegisterVeteranOption, Converter={StaticResource BoolToVisibilityConverter}}">
<StackPanel Orientation="Horizontal">
<Viewbox Height="20"
Width="20">
<Path Data="M9.6110058,5.399L11.650064,5.399 11.650064,9.6789093 15.929999,9.6789093 15.929999,11.717999 11.650064,11.717999 11.650064,15.998 9.6110058,15.998 9.6110058,11.717999 5.3309995,11.717999 5.3309995,9.6789093 9.6110058,9.6789093z M10.66645,1.6420071C5.6808301,1.6420071 1.6404767,5.6823504 1.6404767,10.66655 1.6404767,15.650881 5.6808301,19.691223 10.66645,19.691223 15.64948,19.691223 19.689723,15.650881 19.689723,10.66655 19.689723,5.6823504 15.64948,1.6420071 10.66645,1.6420071z M10.66645,0C16.546567,0 21.333001,4.785153 21.333001,10.66655 21.333001,16.548068 16.546567,21.333 10.66645,21.333 4.7836227,21.333 0,16.548068 0,10.66655 0,4.785153 4.7836227,0 10.66645,0z"
Fill="{DynamicResource StyleBasedForegroundColor}" />
</Viewbox>
<TextBlock Text="Register"
Margin="10,0,0,0"
Foreground="{DynamicResource StyleBasedForegroundColor}"
VerticalAlignment="Center" />
</StackPanel>
</Button>
Although, things work when the app is running, I get this annoying warning:
The resource "StyleBasedForegroundColor" could not be resolved.
I looked up other questions, but could not find a way that doesn't involve themes.xaml or ResourceDictionary

WPF DataGrid ColumnHeaders Scroll Out of Sync with Data Cells

The ColumnHeaders of my WPF DataGrid get out of sync with the data cells when scrolling horizontally. It appears that the Columnheaders scroll faster than the data cells. Vertical scrolling is workig ok. This is the View, which sizes the width of the DataGrid according to the Window size:
<Window.DataContext>
<vm:DemoDgViewModel/>
</Window.DataContext>
<Grid x:Name="dGrid" Height="300" Margin="15,0,15,0" Background="AliceBlue" Width="1000">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="32"/>
<RowDefinition Height="32"/>
<RowDefinition Height="32"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="demoDG" Width="{Binding Path=Width,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
ItemsSource="{Binding Path = DataContext.AppDataLcv,
RelativeSource={RelativeSource FindAncestor, AncestorType=
{x:Type Window}}}" Style ="{DynamicResource StDataGrid2}"
Grid.Row="0" Grid.RowSpan="6" Height="300" AlternationCount ="2"
IsSynchronizedWithCurrentItem="True" SelectionMode ="Single"
AutoGenerateColumns="False" CanUserAddRows="True" CanUserSortColumns ="True"
ScrollViewer.CanContentScroll="True" ColumnWidth="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="desciption" SortMemberPath="desc"
Binding ="{Binding Path=desc}" IsReadOnly="False" CanUserSort="True"
Width ="200">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Padding" Value="5,0,0,0"/>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Padding" Value="5,0,0,0"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>
<!-- column 2, 3, 4, 5 same as the first -->
</DataGrid.Columns>
</DataGrid>
</Grid>
And this is the DataGrid style:
<!-- DataGrid Styles ============ -->
<Style x:Key="StDataGrid2" TargetType="DataGrid" >
<Setter Property="RowHeaderWidth" Value="0" />
<Setter Property="RowHeight" Value="32" />
<Setter Property="RowBackground" Value="PowderBlue" />
<Setter Property="FontWeight" Value="Regular" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="ScrollViewer.CanContentScroll" Value="True" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ColumnHeaderStyle" Value ="{DynamicResource stDghdr}" />
<Setter Property="Margin" Value="0,0,10,0" />
<Setter Property="Template">
<Setter.Value >
<ControlTemplate >
<Border BorderThickness="1" BorderBrush="#FF60727B">
<DockPanel Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type Window}}}">
<StackPanel DockPanel.Dock="Top" Height="15" Orientation="Horizontal"
HorizontalAlignment="Left" CanHorizontallyScroll="False" Width="200" >
<RepeatButton x:Name="LineLeftButton" Width="90" Content="<"
Interval="1000" Delay="1000"
Command="{x:Static ScrollBar.LineLeftCommand}"
CommandTarget="{Binding ElementName=dgscrollviewer}"ClickMode="Hover" />
<RepeatButton x:Name="LineRightButton" Width="90" Content=">"
Interval="1000" Delay="1000"
Command="{x:Static ScrollBar.LineRightCommand}"
CommandTarget="{Binding ElementName=dgscrollviewer}" ClickMode="Hover"/>
</StackPanel>
<ScrollViewer x:Name="dgscrollviewer" CanContentScroll ="True"
VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility ="Auto">
<StackPanel Orientation="Vertical" Height="Auto"
HorizontalAlignment="Stretch" CanHorizontallyScroll="True">
<DataGridColumnHeadersPresenter HorizontalAlignment="Stretch"
Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type Window}}}"
HorizontalContentAlignment="Stretch" Height= "32" Padding="0" Margin="0" />
<ItemsPresenter Height="300" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
</StackPanel>
</ScrollViewer>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="stDghdr" TargetType="DataGridColumnHeader" >
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value ="Stretch"/>
<Setter Property="Height" Value="25" />
<Setter Property="SeparatorBrush" Value="#79858b" />
<Setter Property="Padding" Value="5,0,5,0" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Background" Value ="LavenderBlush"/>
<Setter Property="BorderBrush" Value="#FF60727B"/>
<Setter Property="BorderThickness" Value="2,0,2,2"/>
</Style>
Here is a link to download the demo:
Demo
*Edit/ Update: I think the ColumnHeaders are scrolling faster because they are controlled by a different ScrollViewer? For instance, if I set a large FontSize property on the ScrollViewer that is in the DataGrid style, the text in the DataGrid cells gets bigger but not the text in the ColumnHeaders.
Any ideas or suggestions besides creating a ControlTemplate for the entire DataGrid? Thank you.

WPF style triggers on a TreeView?

I have the following treeview defined in my xaml:
<TreeView Name="PST_TreeView"
Grid.Row="0"
Grid.Column="0"
Width="Auto"
Height="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{Binding SitesCollection}"
ItemTemplate="{StaticResource SitesTemplate}"
Style="{StaticResource TreeViewStyleBasic}" />
With Resource bindings targeting my resources file:
<Style x:Key="TreeViewStyleBasic" TargetType="TreeView">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="{DynamicResource TitleBarButtons_BorderBrush}" />
<Setter Property="BorderThickness" Value="0 0 2 0" />
</Style>
<Style x:Key="TreeViewItemStyle_CatNodes" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Snow" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontSize" Value="16" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="TextAlignment" Value="Left" />
</Style>
<Style x:Key="TreeViewItemStyle_ChildNodes" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Snow" />
<Setter Property="FontFamily" Value="Calibri" />
<Setter Property="FontSize" Value="14" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontStyle" Value="Italic" />
<Setter Property="TextAlignment" Value="Left" />
</Style>
<DataTemplate x:Key="VolumeInfoDataTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Margin="5"
Style="{DynamicResource TreeViewItemStyle_ChildNodes}"
Text="{Binding VolumeName}" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="SitesTemplate"
ItemsSource="{Binding VolumesList}"
ItemTemplate="{StaticResource VolumeInfoDataTemplate}">
<StackPanel Orientation="Horizontal">
<TextBlock Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Margin="5"
Style="{DynamicResource TreeViewItemStyle_CatNodes}"
Text="{Binding SiteName}" />
</StackPanel>
</HierarchicalDataTemplate>
The xaml and resource look ups above work find and as expected.
How might I employ triggers to extend my style definitions to say for example handle the 'IsSelected' event so that the selected tree node will have a slate grey border and a light grey background?
RESEARCH: Kind of thing I am going for.
UPDATE: There is no IsSelected property on the TreeView, however TreeViewItem does has one defined.
Try this:
<DataTemplate x:Key="VolumeInfoDataTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Margin="5"
Style="{DynamicResource TreeViewItemStyle_ChildNodes}"
Text="{Binding VolumeName}"
Name="Tb" />
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=TreeViewItem}}" Value="True">
<Setter TargetName="Tb" Property="Background" Value="LightGray"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>

WPF Design-Time vs Run-Time Style Differences with Triggers

I am having a big issue with how XAML is rendered in Design-Time vs Run-Time. For the most part, things are consistent, but when I use any styles which have a Trigger, the trigger is not checked in Design-Time.
Here is a sample application to show how things are displayed differently:
<Window x:Class="DesignDifferencesWithDesignAndRuntime.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="400" Width="400">
<Window.Resources>
<Style x:Key="multiLineInTrigger" TargetType="{x:Type TextBox}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="22" />
<Setter Property="BorderBrush" Value="Blue" />
<Setter Property="BorderThickness" Value="2" />
<Style.Triggers>
<Trigger Property="AcceptsReturn" Value="True">
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="Auto" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="singleLineInTrigger" TargetType="{x:Type TextBox}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Width" Value="Auto" />
<Setter Property="Height" Value="Auto" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="BorderBrush" Value="Blue" />
<Setter Property="BorderThickness" Value="2" />
<Style.Triggers>
<Trigger Property="AcceptsReturn" Value="False">
<Setter Property="Width" Value="150" />
<Setter Property="Height" Value="22" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Single (Single Style)" Grid.Row="0" Grid.Column="0" />
<TextBlock Text="Single (Multi Style)" Grid.Row="1" Grid.Column="0" />
<TextBlock Text="Multi (Single Style)" Grid.Row="2" Grid.Column="0" />
<TextBlock Text="Multi (Multi Style)" Grid.Row="3" Grid.Column="0" />
<TextBox Grid.Row="0" Grid.Column="1" Style="{StaticResource singleLineInTrigger}" />
<TextBox Grid.Row="1" Grid.Column="1" Style="{StaticResource multiLineInTrigger}" />
<TextBox Grid.Row="2" Grid.Column="1" Style="{StaticResource singleLineInTrigger}" AcceptsReturn="True" />
<TextBox Grid.Row="3" Grid.Column="1" Style="{StaticResource multiLineInTrigger}" AcceptsReturn="True" />
</Grid>
I created two separate TextBox styles which do the exact same thing. When the TextBox is Single-Line (AcceptsReturn = False) I need the width to be 150, and the height to be 22. When it is Multi-Line (AcceptsReturn = True, obviously) I need the width and height to stretch and take up the entire space.
Both of these triggers work perfectly in Run-Time, as running this code will show, but in Design-Time they both fail to work on the trigger condition. When using the "multiLineInTrigger" style, the textbox will have the height and width set statically (regardless of AcceptsReturn), but when using the "singleLineInTrigger" style, the controls will be stretched regardless of AcceptsReturn value.
Is there a solution for this issue? This has become quite troublesome and time-consuming for the development team because they do not know when it is working vs when it is not until compiling and running the application (which is a lengthy process).
Thanks.
I've seen this problem many times and I've never seen a workaround for it, Triggers doesn't work in Visual Studio 2010 Designer. Hopefully they can get this fixed soon.
The only solution I can think of is to do the design work in Expression Blend 4 instead where this works perfectly. Might not be ideal but at the moment I don't think you have any other choise

Resources