WPF 将 Width 绑定到 Parent.Width*0.3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20326744/
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-09 00:01:00  来源:igfitidea点击:

WPF binding Width to Parent.Width*0.3

wpfdata-bindingscale

提问by mandarin

I want to bind a control's Width to the parent's Width, but to a certain scale. Is there a way to do something like this:

我想将控件的宽度绑定到父级的宽度,但要绑定到一定的比例。有没有办法做这样的事情:

<Rectangle  Name="rectangle1" Width="{Binding ActualWidth*0.3, ElementName=thumbnailCanvas, UpdateSourceTrigger=PropertyChanged}" Height="{Binding ActualHeight, ElementName=thumbnailCanvas, UpdateSourceTrigger=PropertyChanged}"/>

回答by Somedust

Sure, but you will need to use a converter. Something like this one:

当然可以,但您需要使用转换器。像这样的东西:

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace WpfTestBench.Converters
{
    public class PercentageConverter : MarkupExtension, IValueConverter
    {
        private static PercentageConverter _instance;

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToDouble(value) * System.Convert.ToDouble(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return _instance ?? (_instance = new PercentageConverter());
        }
    }
}

And your XAML will look like:

您的 XAML 将如下所示:

<Window x:Class="WpfTestBench.ScaleSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:converters="clr-namespace:WpfTestBench.Converters"
        Title="Scale sample" Height="300" Width="300">
    <Grid Name="ParentGrid">
        <Rectangle
            Width="{Binding Path=ActualWidth, ElementName=ParentGrid, Converter={converters:PercentageConverter}, ConverterParameter='0,5'}"
            Stroke="Black" StrokeThickness="2" />
    </Grid>
</Window>

回答by Garreye

I'd recommend simply doing this in XAML using a grid columns and the * width type:

我建议使用网格列和 * 宽度类型在 XAML 中简单地执行此操作:

<Window x:Class="NameSpace.WindowName"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0"></Grid><!--This item take up 1/3 of window width-->
        <Grid Grid.Column="1"></Grid> <!--This item take up remaining 2/3 of window width-->

    </Grid>
</Window>

You can change the ratio of how much the columns take up by changing the numbers before the * in the column widths. Here it's setup as 1 and 2, so the grid will be split in 3 (sum of all * widths), with 1/3 of width to first column and 2/3 to second column.

您可以通过更改列宽中 * 之前的数字来更改列所占的比例。这里设置为 1 和 2,所以网格将被分成 3 个(所有 * 宽度的总和),第一列的宽度为 1/3,第二列的宽度为 2/3。