2010-06-05 3 views
26

저는 WPF를 배우기 때문에 제 단추가 사각형 모양을 유지하는 방법을 알아낼 수 없습니다. 여기 WPF 동적 레이아웃 : 사각형 비율을 적용하는 방법 (너비는 높이와 같음)?

내 XAML 마크 업입니다 : WidthHeight 속성에 대한 명시 적 값을 지정

<Window x:Class="Example" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Height="368" Width="333"> 
    <Window.Resources> 
    <Style x:Key="ToggleStyle" BasedOn="{StaticResource {x:Type ToggleButton}}" 
          TargetType="{x:Type RadioButton}"> 
    </Style> 
    </Window.Resources> 
    <RadioButton Style="{StaticResource ToggleStyle}"> 
     Very very long text 
    </RadioButton> 
</Window> 

하는 것은 잘못된 생각입니다 - 버튼은 자동적으로 그 내용에 따라 그 크기를 계산하지만, 그 폭과 동일한 높이를 유지해야 . 이것이 가능한가?

+0

http://stackoverflow.com/questions/288954/how-do-i-keep-aspect-ratio-on-scalable-scrollable-content-in-wpf : 스타일로 t은 예를 맞게 – Mangesh

답변

36

이 시도 - Kaxaml에서 작동하는 것 같다 :

<Button 
    MinWidth="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" 
    MinHeight="{Binding ActualWidth, RelativeSource={RelativeSource Self}}"> 
    Some content 
</Button> 

가 (테스트하려면, 내가 넣어 단추 내부의 TextBox는 Xaml을 다시 파싱하지 않고도 내용 크기를 쉽게 변경할 수 있기 때문에 가능합니다.)

편집 : 죄송합니다.

<Style TargetType="Button" x:Key="SquareButton"> 
    <Setter Property="MinWidth" Value="{Binding ActualHeight, RelativeSource={RelativeSource Self}}" /> 
    <Setter Property="MinHeight" Value="{Binding ActualWidth, RelativeSource={RelativeSource Self}}" /> 
</Style> 
+0

max 대신 max를 사용하는 방법이 있습니까? –

+0

별도의 질문을해야합니다. 네가 뭘 하려는지 확신 할 수 없어. 이 스 니펫은 내용을 둘러싼 버튼을 사각형으로 만듭니다. 달성하려는 것은 무엇입니까? –

+0

답변을 작성했습니다 : http://stackoverflow.com/questions/29620938/windows-phone-xaml-force-square-proportioned-container-aspectratio-11 –

4

난 당신이 같은 높이로 버튼의 폭을 바인딩 할 생각 :

<Button Name="myButton" 
Width="{Binding ElementName=myButton, Path=Height}" 
Height="100"> 
Button Text 
</Button> 
+4

Gart는 콘텐츠로 자동 크기 지정하기를 원한다고 생각합니다. 이는 약간 까다 롭습니다. 또한,'ElementName'을 사용하는 대신, RelativeSource = {RelativeSource Self}를 사용할 수 있습니다. –

+0

@ Dan Puzey, Thanks =) – n535

관련 문제