8

デザイン時と実行時の 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>

まったく同じことを行う 2 つの別個の TextBox スタイルを作成しました。TextBox が単一行 (AcceptsReturn = False) の場合、幅を 150、高さを 22 にする必要があります。複数行 (AcceptsReturn = True、明らかに) の場合、幅と高さを伸ばして取る必要があります。空間全体をアップ。

このコードを実行するとわかるように、これらのトリガーは両方ともランタイムでは完全に機能しますが、デザインタイムでは両方ともトリガー条件で機能しません。「multiLineInTrigger」スタイルを使用する場合、テキスト ボックスの高さと幅は (AcceptsReturn に関係なく) 静的に設定されますが、「singleLineInTrigger」スタイルを使用する場合、コントロールは AcceptsReturn 値に関係なく引き伸ばされます。

この問題の解決策はありますか? これは、アプリケーションをコンパイルして実行するまで (時間のかかるプロセスである) いつ機能しているか、いつ機能していないかがわからないため、開発チームにとって非常に面倒で時間のかかるものになりました。

ありがとう。

4

1 に答える 1

6

この問題は何度も見てきましたが、回避策を見たことがありません。トリガーはVisual Studio 2010 Designer では機能しません。うまくいけば、彼らはこれをすぐに修正することができます.

私が考えることができる唯一の解決策は、これが完全に機能する代わりに、 Expression Blend 4で設計作業を行うことです。理想的ではないかもしれませんが、現時点では他に選択肢はないと思います

于 2011-01-04T22:09:53.843 に答える