2010-09-26 95 views
17

说我有一个元素中的2个按钮,我想设置2个元素始终填充它的每个元素的1/2宽度,我可以这样做吗?WPF:我可以通过百分比设置元素的宽度吗?

UPDATE

为什么不能我做这样的事情

<StackPanel Orientation="Horizontal" Grid.Row="0"> 
    <Button Content="Click me" Command="{Binding ClickCommand}" Width="1*" /> 
    <Button Content="Exit" Command="{Binding CloseCommand}" Width="1*" /> 
</StackPanel> 

为什么犯规在此背景下,1 *的工作?我得到的错误

无法将“1 *”

+1

它不起作用,因为您使用的是StackPanel。星形尺寸仅适用于网格。 – ASanch 2010-09-26 04:24:23

+3

哦,特别是星形尺寸只能在ColumnDefinition.Width属性或RowDefinition.Height属性上完成。它不能在子控件上设置(就像你上面用Button的Width属性所做的那样)。 – ASanch 2010-09-26 04:26:38

回答

43

您可以使用Grid有两列这一点。

<Grid> 
    <Grid.ColumnDefinitions> 
    <ColumnDefinition Width="1*"/> 
    <ColumnDefinition Width="1*"/> 
    </Grid.ColumnDefinitions> 

    <Button Grid.Column="0">Button1</Button> 
    <Button Grid.Column="1">Button2</Button> 
</Grid> 

请注意,在ColumnDefinition.Width财产使用星(*)。这意味着两列将占用相同数量的空间。所以在上面的例子中,每个按钮将占用包含的Grid的可用空间的1/2。因此,如果您使一个Width等于2*,那么该列将占用另一列的两倍的空间量。希望这是有道理的。

+0

我忘了所有关于* thingy!谢谢 – 2010-09-26 01:41:34

+0

哦,看我的更新 – 2010-09-26 04:22:44

1

我创建了一个ContentControl,允许我包装内容添加动态百分比宽度/高度。

/// <summary> 
/// This control has a dynamic/percentage width/height 
/// </summary> 
public class FluentPanel : ContentControl, IValueConverter 
{ 
    #region Dependencie Properties 

    public static readonly DependencyProperty WidthPercentageProperty = 
     DependencyProperty.Register("WidthPercentage", typeof(int), typeof(FluentPanel), new PropertyMetadata(-1, WidthPercentagePropertyChangedCallback)); 

    private static void WidthPercentagePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
    { 
     ((FluentPanel)dependencyObject).OnWidthPercentageChange(); 
    } 

    public int WidthPercentage 
    { 
     get { return (int)GetValue(WidthPercentageProperty); } 
     set { SetValue(WidthPercentageProperty, value); } 
    } 

    public static readonly DependencyProperty HeightPercentageProperty = 
     DependencyProperty.Register("HeightPercentage", typeof(int), typeof(FluentPanel), new PropertyMetadata(-1, HeightPercentagePropertyChangedCallback)); 

    private static void HeightPercentagePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) 
    { 
     ((FluentPanel)dependencyObject).OnHeightPercentageChanged(); 
    } 

    public int HeightPercentage 
    { 
     get { return (int)GetValue(HeightPercentageProperty); } 
     set { SetValue(HeightPercentageProperty, value); } 
    } 

    #endregion 

    #region Methods 

    private void OnWidthPercentageChange() 
    { 
     if (WidthPercentage == -1) 
     { 
      ClearValue(WidthProperty); 
     } 
     else 
     { 
      SetBinding(WidthProperty, new Binding("ActualWidth") { Source = Parent, Converter = this, ConverterParameter = true }); 
     } 
    } 

    private void OnHeightPercentageChanged() 
    { 
     if (HeightPercentage == -1) 
     { 
      ClearValue(HeightProperty); 
     } 
     else 
     { 
      SetBinding(HeightProperty, new Binding("ActualHeight") { Source = Parent, Converter = this, ConverterParameter = false }); 
     } 
    } 

    #endregion 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if ((bool)parameter) 
     { 
      // width 
      return (double)value * (WidthPercentage * .01); 
     } 
     else 
     { 
      // height 
      return (double)value * (HeightPercentage * .01); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 
相关问题