WPF设计时与运行时风格与触发器的差异 [英] WPF Design-Time vs Run-Time Style Differences with Triggers

查看:96
本文介绍了WPF设计时与运行时风格与触发器的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在设计时与运行时如何呈现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 = Horizo​​ntalAlignment Value =左 />
< 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 = Horizo​​ntalAlignment Value = Stretch />
< Setter Property = VerticalAlignment Value = Stretch />
< / Trigger>
< /Style.Triggers>
< / Style>
< Style x:Key = singleLineInTrigger TargetType = {x:Type TextBox}>
< Setter Property = Horizo​​ntalAlignment Value =左 />
< Setter Property = Width Value = Auto />
< Setter Property = Height Value = Auto />
< Setter Property = Horizo​​ntalAlignment 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 = Horizo​​ntalAlignment Value =左 />
< Setter Property = VerticalAlignment Value = Center />
< / Trigger>
< /Style.Triggers>
< / Style>
< Style TargetType = {x:Type TextBlock}>
< Setter Property = FontWeight Value = Bold />
< Setter Property = Horizo​​ntalAlignment 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 =单个(单一样式) Grid.Row = 0 Grid.Column = 0 />
< TextBlock Text =单个(多种样式) Grid.Row = 1 Grid.Column = 0 />
< TextBlock Text =多(单一样式) Grid.Row = 2 Grid.Column = 0 />
< TextBlock Text =多(多样式) 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是Single-Line(AcceptsReturn = False)时,我需要将宽度设置为150,将高度设置为22。当它是Multi-Line(AcceptsReturn = True,显然)时,我需要使用宽度和高度来拉伸和获取



这两个触发器在运行时都可以完美运行,正如运行此代码将显示的那样,但是在设计时它们都无法在运行时工作。触发条件。使用 multiLineInTrigger样式时,文本框将具有静态设置的高度和宽度(与AcceptsReturn无关),但是当使用 singleLineInTrigger样式时,控件将被拉伸而与AcceptsReturn值无关。 b

是否有解决此问题的方法?对于开发团队来说,这已经变得相当麻烦且耗时,因为他们不知道何时该工作,什么时候不知道,直到编译并运行该应用程序(这是一个漫长的过程)。



谢谢。

解决方案

我已经多次看到此问题,但从未见过针对该问题的解决方法它,触发器在 Visual Studio 2010 设计器中不起作用。希望他们能尽快解决此问题。



我能想到的唯一解决方案是在 Expression Blend 4 中进行设计工作,而不是在这种情况下效果最佳。可能并不理想,但目前我认为您没有其他选择


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

这篇关于WPF设计时与运行时风格与触发器的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆