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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3796160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:09:20  来源:igfitidea点击:

WPF: Can i set the width of an element by percentages?

wpf

提问by Jiew Meng

Say I have 2 buttons in an Element and I want to set the 2 elements to always fill up 1/2 width of its containing element each, can i do that?

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

UPDATE

更新

why cant i do something like

为什么我不能做类似的事情

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

why doesnt the 1* work in this context? i get the error

为什么 1* 在这种情况下不起作用?我得到了错误

Cannot convert "1*"

无法转换“1*”

回答by ASanch

You can use a Gridwith two columns for this.

Grid为此,您可以使用带有两列的a 。

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

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

Notice the use of star(*) in the ColumnDefinition.Widthproperty. This means that both columns will take up the same amount of space. So in the example above, each button will each occupy 1/2 of the available space of the containing Grid. So if you make one Widthto be equal to 2*, that column will take up twice the amount of space as the other column. Hope this makes sense.

注意属性中 star( *)的使用ColumnDefinition.Width。这意味着两列将占用相同的空间量。所以在上面的例子中,每个按钮都将占据包含 1/2 的可用空间Grid。因此,如果您将一列Width设置为等于2*,则该列将占用另一列两倍的空间。希望这是有道理的。

回答by Paul Knopf

I created a ContentControl that allows me wrap content to add a dynamic percentage width/height.

我创建了一个 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();
    }
}