I'm having a big problem with how XAML is displayed in Design-Time vs Run-Time. For the most part, everything is consistent, but when I use any styles that have a trigger, the trigger is not checked in Design-Time.
Here is an example application showing 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 that do the same . When the TextBox is single-line (AcceptsReturn = False), I need a width of 150, and the height should be 22. If it's Multi-Line (AcceptsReturn = True, obviously), I need the width and height to stretch and be accepted throughout the space.
Both of these triggers work fine at runtime, as this code is shown, but during development they both do not work on the trigger condition. When using the multiLineInTrigger style, the text field will have a height and width set statically (regardless of AcceptsReturn), but when using the singleLineInTrigger style, the controls will be stretched regardless of the AcceptsReturn value.
Is there a solution for this problem? This has become quite unpleasant and time-consuming for the development team, because they do not know when it works, and not before compiling and running the application (which is a lengthy process).
Thanks.
Travis
source share