2012-02-21 33 views
1

我有一個用戶控件,它只是一個文本這樣的對話框:在我的主窗口WPF用戶控件的寬度和高度不Taknig

<Border x:Name="box" 
      BorderThickness="0" 
      BorderBrush="Black" 
      Background="Black" 
      Width="200" Height="200"> 

    <Label Content="Hello World" 
       Height="65" Width="400" 
       Foreground="White" FontSize="32" 
       HorizontalAlignment="Center" VerticalAlignment="Center" 
       HorizontalContentAlignment="Center" /> 
</Border> 

當我創建和實例吧像這樣...

<local:MyBoxControl Width="1000" Height="1000"/>

寬度和高度200x200,如Border中所定義的,當我希望它創建時爲1000x1000。

回答

1

難道是可以接受的您設置的Horizo​​ntalAlignment和邊界的VerticalAlignment爲Stretch?

+0

這是沿着w/@Joel Lucsy的建議 – Ternary 2012-02-21 18:26:05

4

只需從邊框元素中移除寬度和高度即可。

+0

謝謝你,沿着對齊的東西固定它。 – Ternary 2012-02-21 18:26:22

1

假設您需要的邊框的寬度/高度保持在200(否則你只是不會指定它),你可以做一個網格控制的最外層元素,並把邊界裏面。然後如果你沒有定義寬度/高度,它會自動調整到你的控件的大小。

<Grid> 
    <Border x:Name="box" 
       BorderThickness="0" 
       BorderBrush="Black" 
       Background="Black" 
       Width="200" Height="200"> 

     <Label Content="Hello World" 
        Height="65" Width="400" 
        Foreground="White" FontSize="32" 
        HorizontalAlignment="Center" VerticalAlignment="Center" 
        HorizontalContentAlignment="Center" /> 
    </Border> 
</Grid>