디자인 타임과 런타임에서 XAML이 렌더링되는 방식에 큰 문제가 있습니다. 대부분의 경우 일관성이 있지만 트리거가있는 스타일을 사용하면 트리거가 디자인 타임에 확인되지 않습니다. WPF 디자인 타임과 런타임 스타일과 트리거의 차이점
<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가 Single-Line 일 때 (AcceptsReturn = False) 너비가 150, 높이가 22가되어야합니다. 다중 선 (AcceptsReturn = True, 분명히) 인 경우 늘어나고 걸릴 폭과 높이가 필요합니다. 전체 공간을.
이 코드를 실행하면 런타임에 완벽하게 작동하지만 디자인 타임에서는 트리거 조건에서 작동하지 않습니다. "multiLineInTrigger"스타일을 사용하는 경우 텍스트 상자의 높이 및 너비는 AcceptsReturn과 관계없이 정적으로 설정되지만 "singleLineInTrigger"스타일을 사용하면 AcceptsReturn 값과 관계없이 컨트롤이 늘어납니다.
이 문제에 대한 해결책이 있습니까? 개발 팀은 애플리케이션이 컴파일되고 실행될 때까지 (오랜 프로세스이므로) 언제 작동하는지 알 수 없기 때문에 상당히 번거롭고 시간이 오래 걸립니다.
감사합니다.
VS2010과 Expression Blend는 디자인 타임으로 가정하고 있습니까? –
1) 솔루션을 다시 빌드하고 확인하십시오. 이는 첫 번째 옵션입니다. 2) TextBox의 하위 클래스로 CustomTextBox를 만듭니다. OnApplyTemplate에서이 스타일을 코드 블록 DesignerProperties.GetIsInDesignMode() 내에 적용합니다. 따라서이 스타일은 단독으로 디자인 모드에 적용됩니다. 이 CustomTextBox를 사용하십시오. 너 끝났어. –
@Aaron, 예, 이것은 Visual Studio 2010에서 표현식이 아닙니다. – Travis