【问题标题】:Set Grid Column Width to Bound Perctange将网格列宽设置为绑定百分比
【发布时间】:2013-05-16 20:00:01
【问题描述】:

我想根据绑定的数据源显示带有动态WidthRectangle。我最初考虑使用Converter,但无法绑定到转换器参数以获得读取动态宽度。

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

我该怎么做呢?我希望我的Rectangle 或父列根据 UtilPct 中的百分比占据总列宽的百分比。我对 WPF 还是很陌生,所以我很感激任何帮助!提前致谢。

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>

【问题讨论】:

    标签: wpf xaml datatemplate itemscontrol ivalueconverter


    【解决方案1】:

    你可以使用IMultiValue转换器,这样你可以传入WidthPrecentage,这样你就可以计算列的宽度了。

    例子:

    转换器:

    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>
    

    【讨论】:

    • 不错!我什至不知道多重绑定!谢谢@sa_ddam213
    • @ss_ddam213 - 看起来我不能多次设置 Width,所以我尝试使用 columndefinition 的宽度 - 但它没有传入实际的宽度值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    • 2013-10-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 2012-10-10
    • 2013-08-28
    相关资源
    最近更新 更多