【问题标题】:Bind width of UI element to a percentage of another UI element's width将 UI 元素的宽度绑定到另一个 UI 元素宽度的百分比
【发布时间】:2013-04-01 17:15:33
【问题描述】:

基于this question,但略有不同。

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

例如:

<ScrollViewer x:Name="scrollviewerWrapper">

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

</ScrollViewer>

因此,如果我的 ScrollViewer 为 100px,我希望我的 TextBlock 为 60% = 60px(scrollviewer 的宽度是动态的)。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

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

    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);
            }
    

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 2018-10-15
      • 2019-04-28
      • 1970-01-01
      • 2016-10-24
      • 2012-04-22
      • 1970-01-01
      • 2019-06-18
      • 2017-12-31
      相关资源
      最近更新 更多