2010-07-31 3 views
6

내 WPF UserControl에는 두 개의 스택 패널이 포함되어 있으며 각 패널에는 레이블, 텍스트 상자 및 라디오 단추가 포함되어 있습니다.
가능한 작은 코드로 내 UserControl의 모든 컨트롤에 CenterVerticalAlignment 속성을 설정하고 싶습니다.VerticalAlignment 속성을 모든 컨트롤로 설정

는 이제 다음과 같은 솔루션이 있습니다

  • 무력을 -
  • FrameworkElement에 대해 하나 개의 스타일을 정의하고
  • 사용자 컨트롤의 컨트롤의 각 유형의 스타일을 정의 직접 적용 각 컨트롤에 VerticalAlignment="Center"를 넣어 (3 가지 스타일 정의가 필요하지만 컨트롤에 스타일이 자동으로 적용됩니다.)

이러한 세 가지 솔루션은 너무 많은 코드가 필요합니다.
다른 방법으로 쓸 수 있나요?
FrameworkElement의 스타일을 정의하면 자동으로 모든 컨트롤에 속성이 설정되기를 기대했지만 작동하지 않습니다. 여기

(나는, 두 번째 매우 유사한 스택 패널을 생략) 내 현재 XAML의 조각입니다 :

<UserControl.Resources> 
    <Style x:Key="BaseStyle" TargetType="FrameworkElement"> 
     <Setter Property="VerticalAlignment" Value="Center" /> 
    </Style> 
</UserControl.Resources> 
<Grid> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Style="{StaticResource BaseStyle}" Text="Value:" /> 
     <RadioButton Style="{StaticResource BaseStyle}">Standard</RadioButton> 
     <RadioButton Style="{StaticResource BaseStyle}">Other</RadioButton> 
     <TextBox Style="{StaticResource BaseStyle}" Width="40"/> 
    </StackPanel> 
</Grid> 

편집 :
가 다시 윌의 코멘트 : 정말 코드 숨김 코드를 포맷 제어를 작성하는 생각이 싫어 . XAML은이 간단한 사용자 제어에 충분해야합니다.

Re Muad'Dib 님의 의견 : 내 사용자 정의 컨트롤에서 사용하는 컨트롤은 FrameworkElement에서 파생되므로 여기에서 문제가되지 않습니다.

+0

겠습니까 :이 "최고"방법이지만 기본 스타일에서 상속 페이지의 각 컨트롤에 대해 별도의 스타일을 만들어 다음 기본 스타일을 정의하고 의해 관리 할 수있을만큼 쉽다 확실하지 코드 숨김으로 생각하면 내가 생각하는 것처럼 끔찍한 생각일까요? –

+0

모든 컨트롤이 FrameworkElement에서 상속하지 않습니다. –

답변

9

나는 전에도 같은 수수께끼를 발견했다. 이 설정

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="500" Height="300" Background="OrangeRed"> 

<Page.Resources> 
    <Style TargetType="FrameworkElement" x:Key="BaseStyle"> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
    <Setter Property="Margin" Value="0,0,5,0" /> 
    </Style> 

    <Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}" /> 
    <Style TargetType="RadioButton" BasedOn="{StaticResource BaseStyle}" /> 
    <Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}" /> 
</Page.Resources> 

<Grid> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="Value:" /> 
     <RadioButton>Standard</RadioButton> 
     <RadioButton>Other</RadioButton> 
     <TextBox Width="75"/> 
    </StackPanel> 
</Grid> 

</Page> 
+0

그래도 세 줄은 리소스가 많지만 컨트롤은 덜 복잡합니다. 현재 코드보다 좋아 보인다. – zendar

관련 문제