2013-12-13 2 views
0

Silverlight를 사용하여 두 개의 TextBlock을 추가하고자하는 버튼이 있습니다. 하나는 완전히 상위 컨테이너에 중점을두고 다른 하나는 상위 컨테이너의 오른쪽 하단에 있습니다 (아래 그림 참조). 오래된 WinForms에서는 이것을 달성하기 위해 앵커를 사용하는 것이 쉬웠을 것입니다. 그러나 제한된 수의 Silverlight 컨테이너를 사용하여 동일한 결과를 얻을 수 없습니다.Silverlight에서 WinForm 앵커를 모방하는 방법은 무엇입니까?

이것을 달성하는 방법에 대한 의견이나 제안이 있으십니까? 나는 이것을위한 올바른 컨테이너를 찾고 모든 것이 완벽하게 정렬되도록하는 방법을 찾으려고합니다.

텍스트가 1자를 초과 할 수 있으며 X와 0이 약간 겹칠 수 있습니다 (즉, 3 행 격자를 사용할 수 없음).

enter image description here

답변

1

당신과 같이 그리드에서 서로의 상단에 요소를 쌓을 수 :

<Border BorderThickness="2" BorderBrush="Blue" Width="200" Height="200"> 
     <Grid> 
      <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="X" /> 
      <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="0" /> 
     </Grid> 
    </Border> 

편집

이 예제를 달성하기 위해 사용자 정의 버튼 스타일을 사용 게시 된 결과 :

<Button Width="200" Height="200" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" BorderThickness="6" BorderBrush="SteelBlue" Background="White" Padding="16"> 
     <Button.Style> 
      <Style TargetType="Button"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="Button"> 
          <Grid> 
           <VisualStateManager.VisualStateGroups> 
            <VisualStateGroup x:Name="FocusStates"> 
             <VisualState x:Name="Unfocused"/> 
             <VisualState x:Name="Focused"/> 
            </VisualStateGroup> 
            <VisualStateGroup x:Name="CommonStates"> 
             <VisualState x:Name="MouseOver"/> 
             <VisualState x:Name="Normal"/> 
             <VisualState x:Name="Pressed"/> 
             <VisualState x:Name="Disabled"> 
              <Storyboard> 
               <DoubleAnimation Duration="0" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity" To="1"/> 
              </Storyboard> 
             </VisualState> 
            </VisualStateGroup> 
           </VisualStateManager.VisualStateGroups> 
           <Border x:Name="Background" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" /> 
           <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/> 
           <Rectangle x:Name="DisabledVisualElement" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" /> 
          </Grid> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Button.Style> 
     <Grid> 
      <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="X" FontSize="144" FontWeight="Bold" Foreground="SteelBlue" /> 
      <TextBlock VerticalAlignment="Bottom" HorizontalAlignment="Right" Text="0" FontSize="24" Foreground="SteelBlue" /> 
     </Grid> 
    </Button> 
+0

이것이 작동하는 것 같습니다! 혹시 모눈을 부모 컨테이너 (내 경우에는 버튼)에 채우게하는 방법을 알고 있습니까? – Martin

+0

하위 요소에서 단추를 늘리려면 가로/세로 ContentAlignment를, 가로/세로 정렬을 늘일 수 있습니다. 그러나 기본적으로 속성이 확장으로 설정되어 있으면 안됩니다. 또한 안쪽 여백이나 여백에 내용을 넣지 않았는지 확인하십시오. 예를 들어 스크린 샷에 매우 근접한 단추 스타일과 모든 기본 단추 상태가 제거 된 예제를 업데이트했습니다. 기본 Silverlight 컨트롤 템플릿은 다음 위치에서 얻을 수 있습니다. http://msdn.microsoft.com/ko-kr/library/cc278075%28v=vs.95%29.aspx –

+0

Rob! 감사합니다. 나는 버튼의 HorizontalContentAlignment와 VerticalContentAlignment를 업데이트하는 것을 완전히 잊었다! 방금 해키 한 솔루션을 우아한 제품으로 바꿨습니다. 감사! – Martin

관련 문제