하나의 XAML 스타일에 대해 여러 TargetType을 정의 할 수 있습니까?
HTML / CSS에서는 다양한 유형의 요소에 적용 할 수있는 스타일을 정의 할 수 있습니다. 예 :
.highlight {
color:red;
}
P와 DIV 모두에 적용 할 수 있습니다. 예 :
<p class="highlight">this will be highlighted</p>
<div class="highlight">this will also be highlighted</div>
하지만 XAML에서는 스타일에 대한 TargetType을 정의해야하는 것 같습니다. 그렇지 않으면 오류가 발생합니다.
<Style x:Key="formRowLabel" TargetType="TextBlock">
XAML 스타일을 여러 요소에 적용하거나 CSS 에서처럼 열어 둘 수있는 방법이 있습니까?
WPF 스타일의 setter는 컴파일 시간 동안 확인됩니다. CSS 스타일은 동적으로 적용됩니다.
WPF가 setter의 속성을 해당 형식의 종속성 속성으로 확인할 수 있도록 형식을 지정해야합니다.
대상 유형을 원하는 속성이 포함 된 기본 클래스로 설정 한 다음 해당 스타일을 파생 클래스에 적용 할 수 있습니다. 예를 들어 Control 개체에 대한 스타일을 만든 다음 여러 유형의 컨트롤 (Button, TextBox, CheckBox 등)에 적용 할 수 있습니다.
<Style x:Key="Highlight" TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="Red"/>
</Style>
...
<Button Style="{StaticResource Highlight}" Content="Test"/>
<TextBox Style="{StaticResource Highlight}" Text="Test"/>
<CheckBox Style="{StaticResource Highlight}" Content="Test"/>
<!-- Header text style -->
<Style x:Key="headerTextStyle">
<Setter Property="Label.VerticalAlignment" Value="Center"></Setter>
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Bold"></Setter>
<Setter Property="Label.FontSize" Value="18"></Setter>
<Setter Property="Label.Foreground" Value="#0066cc"></Setter>
</Style>
<!-- Label style -->
<Style x:Key="labelStyle" TargetType="{x:Type Label}">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="0,0,0,5" />
</Style>
스타일을 선언하는 두 가지 방법 모두 귀하의 질문에 답할 수 있다고 생각합니다. 첫 번째에는 TargetType이 지정되어 있지 않지만 속성 이름에는 'Label'접두사가 붙습니다. 두 번째에서는 Label 개체에 대한 스타일이 만들어집니다.
이를 수행하는 또 다른 방법은 다음과 같습니다.
<UserControl.Resources>
<Style x:Key="commonStyle" TargetType="Control">
<Setter Property="FontSize" Value="24"/>
</Style>
<Style BasedOn="{StaticResource commonStyle}" TargetType="ListBox"/>
<Style BasedOn="{StaticResource commonStyle}" TargetType="ComboBox"/>
</UserControl.Resources>
Textblock과 TextBox에 스타일을 적용하고 싶었지만 Textblock이 Control에서 상속하지 않기 때문에 선택한 답변이 작동하지 않았습니다. 제 경우에는 Visibility 속성에 영향을 미치고 싶었으므로 FrameworkElement를 사용했습니다.
<Style x:Key="ShowIfRequiredStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowIfRequiredStyle, UpdateSourceTrigger=PropertyChanged}" Value="true">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBlock Style="{StaticResource ResourceKey=ShowIfRequiredStyle}"/>
<TextBox Style="{StaticResource ResourceKey=ShowIfRequiredStyle}"/>
This works for the Visibility property because both items inherit from Frameworkelement and the property is defined there. Of course this will not work for properties defined only in Control, you can search the hierarchy tree and try to find a base class, anyway I thought this could help someone since this is a top search result and the selected answer is a little incomplete.
There is an alternative answer to the question. You CAN leave the TargetType parameter off the style altogether which will allow it to apply to various different controls, but only if you prefix the property name with "Control."
<Style x:Key="Highlight">
<Setter Property="Control.Foreground" Value="Red"/>
</Style>
Obviously, this only works for properties of the base control class. If you tried to set ItemsSource say, it would fail because there is no Control.ItemsSource
I got this working
<Style x:Key="HeaderStyleThin" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Black" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background=" Value="Red" />
</Style>
</Style.Resources>
</Style>
참고URL : https://stackoverflow.com/questions/802658/can-you-define-multiple-targettypes-for-one-xaml-style
'IT Share you' 카테고리의 다른 글
| 특정 IP에 따라 국가를 얻는 방법은 무엇입니까? (0) | 2020.12.11 |
|---|---|
| API 테스트를위한 cURL 용 GUI 프런트 엔드 (0) | 2020.12.11 |
| Emacs 구문 강조 색상 변경 (0) | 2020.12.11 |
| 문자 배열이 비어 있는지 확인하는 가장 좋은 방법 (0) | 2020.12.11 |
| 매개 변수 IEnumerable (0) | 2020.12.11 |