WPF:我可以按百分比设置元素的宽度吗?

时间:2010-09-26 00:49:21

标签: wpf

假设我在一个元素中有2个按钮,我想将2个元素设置为总是填充其包含元素的1/2宽度,我可以这样做吗?

更新

为什么我不能做像

这样的事情
<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 *”

2 个答案:

答案 0 :(得分:49)

您可以使用带有两列的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*,则该列占用的空间量将是另一列的两倍。希望这是有道理的。

答案 1 :(得分: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();
    }
}