带触发器的WPF设计时与运行时样式差异

带触发器的WPF设计时与运行时样式差异,wpf,triggers,styles,runtime,design-time,Wpf,Triggers,Styles,Runtime,Design Time,我在设计时和运行时如何呈现XAML方面遇到了一个大问题。在大多数情况下,事情是一致的,但是当我使用任何有触发器的样式时,触发器在设计时都不会被检查 下面是一个示例应用程序,用于显示不同的显示方式: <Window x:Class="DesignDifferencesWithDesignAndRuntime.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x

我在设计时和运行时如何呈现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>

我创建了两个独立的文本框样式,它们的功能完全相同。当文本框是单行(AcceptsReturn=False)时,我需要宽度为150,高度为22。当它是多行(显然,AcceptsReturn=True)时,我需要宽度和高度来拉伸并占据整个空间

正如运行此代码所示,这两个触发器在运行时都能完美工作,但在设计时,它们都无法在触发器条件下工作。使用“multiLineInTrigger”样式时,文本框的高度和宽度将静态设置(与AcceptsReturn无关),但使用“singleLineInTrigger”样式时,控件将拉伸,与AcceptsReturn值无关

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


谢谢。

这个问题我已经见过很多次了,我从来没有见过解决方法,触发器在Visual Studio 2010中不起作用。希望他们能很快解决这个问题


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

设计时假设您参考的是VS2010与Expression Blend?1)尝试重新构建解决方案并检查,这是第一个选项。2) 通过子类从TextBox创建CustomTextBox。在OnApplyTemplate中,在代码块DesignerProperties.GetIsInDesignMode()中应用此样式。因此,此样式将单独应用于设计模式。使用此自定义文本框。您完成了。@Aaron,是的,这是在Visual Studio 2010中,不是表达式。@Prince Ashitaka,我已尝试重建解决方案,但没有效果。我们已经有了一些自定义控件,我将尝试处理OnApplyTemplate并使用您的解决方案。这是唯一的解决办法吗?我真的不认为管理层会很乐意为我们用来处理此事件的所有不同控件创建自定义控件。谢谢你提供的信息。我恐怕情况就是这样。感谢您提供的信息,我将查看expression blend,同时看看这是否是一个解决方案。