将“网格列宽”设置为“绑定的垂直度” [英] Set Grid Column Width to Bound Perctange

查看:106
本文介绍了将“网格列宽”设置为“绑定的垂直度”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个 Rectangle ,它基于绑定的数据源具有动态的 Width 。我最初考虑使用 Converter ,但无法绑定到converter参数来获取读取的动态宽度。

I want to display a Rectangle with a dynamic Width based on a bound data source. I originally looked into using a Converter, but wasn't able to bind to the converter parameter to get a read dynamic width.

我最近的尝试是将父列绑定到UtilPct属性,该属性在我的BrokerCredit对象中为小数。我认为这是使用十进制值作为绝对值而不是百分比显示。

My most recent attempt was binding the parent column to the UtilPct property, which is a decimal in my BrokerCredit object. I think this is using the decimal value as an absolute instead of a percentage display.

我该怎么做?我希望我的 Rectangle 或父列根据UtilPct中的百分比占总列宽的百分比。我对WPF还是很陌生,因此感谢您的帮助!

How would I go about doing this? I'd like my Rectangle or the parent column to take up a percentage of the total column width according to the percentage in UtilPct. I'm still pretty new to WPF, so I appreciate any help! Thanks in advance.

XAML:

<ItemsControl x:Name="icBrokerCreditList" Grid.Row="1" Grid.Column="1" ItemsSource="{Binding Path=BrokerCreditList}" HorizontalAlignment="Stretch">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch">
                <Grid.RowDefinitions>
                    <RowDefinition Height="20"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="150"></ColumnDefinition>
                    <ColumnDefinition x:Name="utilizationColumn" Width="{Binding Path=UtilPct}"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Row="0" Grid.Column="0" Background="White" Foreground="Black" FontSize="12" Text="{Binding Path=BrokerName}"></TextBlock>
                <Rectangle Width="auto" Fill="Green" Height="20" Grid.Row="0" Grid.Column="1">
                    <!--"{Binding Converter={StaticResource PercentageConverter}, 
                                        ElementName=utilizationColumn, Path=Width, ConverterParameter=.1}"-->                          
                </Rectangle>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>


推荐答案

您可以使用 IMultiValue 转换器,这样您就可以传递 Width Precentage ,以便您可以计算列的宽度。

You could use IMultiValue converter, this way you can pass in the Width and the Precentage so you can calculate the width of the column.

示例:

转换器:

public class PercentageConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] is double && values[1] is double)
        {
            return (((double)values[0]) / 100) * ((double)values[1]);
        }
        return values[0];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Xaml:

<Rectangle Width="auto" Fill="Green" Height="20" Grid.Row="0" Grid.Column="1">
    <Rectangle.Width>
        <MultiBinding Converter="{StaticResource PercentageConverter}">
            <Binding Path="Width" />
            <Binding Path="UtilPct" />
        </MultiBinding>
    </Rectangle.Width>
</Rectangle>

这篇关于将“网格列宽”设置为“绑定的垂直度”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆