我对如何在设计时与运行时呈现 XAML 有很大的问题。在大多数情况下,事情是一致的,但是当我使用任何具有触发器的样式时,触发器不会在设计时检查。
这是一个示例应用程序,用于显示事物的不同显示方式:
<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>
我创建了两个独立的 TextBox 样式,它们做的事情完全相同。当 TextBox 是单行(AcceptsReturn = False)时,我需要宽度为 150,高度为 22。当它是多行时(AcceptsReturn = True,显然)我需要宽度和高度来拉伸和取整个空间。
这两个触发器在运行时都可以完美地工作,正如运行此代码所显示的那样,但在设计时它们都无法在触发条件下工作。使用“multiLineInTrigger”样式时,文本框的高度和宽度将静态设置(与 AcceptsReturn 无关),但使用“singleLineInTrigger”样式时,无论 AcceptsReturn 值如何,控件都会被拉伸。
这个问题有解决方案吗?这对于开发团队来说变得相当麻烦和耗时,因为他们不知道它什么时候工作,什么时候不工作,直到编译和运行应用程序(这是一个漫长的过程)。
谢谢。