将UI元素的宽度绑定到另一个UI元素宽度的百分比

时间:2013-04-10 02:53:28

标签: wpf xaml

基于this question,但略有不同。

我不确定这是否可行。有没有办法将UI元素的宽度链接到另一个元素的宽度百分比

例如:

<ScrollViewer x:Name="scrollviewerWrapper">

     <TextBlock x:Name="textblock" 
                Width="{Binding Path=Width, [[[ % of ]]] ElementName=scrollviewerWrapper}" />

</ScrollViewer>

所以如果我的ScrollViewer是100px,我希望我的TextBlock是60%的60%(滚动查看器的宽度是动态的)。

1 个答案:

答案 0 :(得分:3)

所以我最终使用转换器来传递数字。只考虑,而不是百分比,我去了总数的剩余部分减去一个固定的宽度,因为我有一个数字。

XAML:

Width="{Binding Path=ActualWidth, ElementName=exampleElement, Converter={StaticResource MyConverter}, ConverterParameter={StaticResource ResourceKey=actionAreaWidth}}">

参数转换:

<clr:Double x:Key="actionAreaWidth">150</clr:Double>

转换器本身:

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            double percentage = 100;
            double.TryParse(parameter.ToString(), out percentage);

            var actualWidth = (double)value;
            return actualWidth * (percentage / 100);
        }
相关问题