WPF设计时与运行时风格的差异与触发器

Tra*_*vis 7 wpf triggers styles design-time runtime

我在设计时与运行时如何呈现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>
Run Code Online (Sandbox Code Playgroud)

我创建了两个单独的TextBox样式,它们完全相同.当TextBox是单行(AcceptsReturn = False)时,我需要宽度为150,高度为22.当它是多行(AcceptsReturn = True,显然)时我需要宽度和高度来拉伸和取整个空间.

这两个触发器在运行时都能完美运行,因为运行此代码会显示,但在设计时它们都无法处理触发条件.使用"multiLineInTrigger"样式时,文本框将静态设置高度和宽度(无论AcceptsReturn如何),但使用"singleLineInTrigger"样式时,无论AcceptsReturn值如何,控件都将被拉伸.

这个问题有解决方案吗?这对于开发团队来说已经变得非常麻烦和耗时,因为他们不知道它何时工作,而不是在编译和运行应用程序之前(这是一个漫长的过程).

谢谢.

Fre*_*lad 5

我已经多次看到这个问题而且我从未见过它的解决方法,触发器在Visual Studio 2010 Designer中不起作用.希望他们能尽快解决这个问题.

我能想到的唯一解决方案是在Expression Blend 4中进行设计工作,而不是完美地工作.可能不理想,但目前我认为你还没有其他选择