【发布时间】:2015-07-27 13:28:49
【问题描述】:
我需要在 ItemsControl 中绘制矩形,其宽度根据绑定集合中定义的值和集合的最大值计算得出。所以我想我需要使用 MultiValueConverter 来传递项目的值和集合的最大值。
这个向转换器添加属性的解决方案here 按原样工作得很好,但当我将视图与 VM 分开时就不行了。
看起来我无法使用 MultiBinding 设置 width 属性 - 我的转换器被调用并返回正确的值,但我看不到矩形:
<ListBox x:Name="list" ItemsSource="{Binding Companies}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CountInvitations}" />
<Rectangle Height="20" Fill="Black">
<Rectangle.Width>
<MultiBinding Converter="{StaticResource myMultiValueConverter}">
<Binding Path="CountInvitations" />
<Binding ElementName="MainLayout" Path="DataContext.MaxCount" />
</MultiBinding>
</Rectangle.Width>
</Rectangle>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是转换器:
int CellWidth = 200;
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int count = int.Parse(values[0].ToString());
int maxCount = int.Parse(values[1].ToString());
var width = CellWidth / maxCount * count;
return width;
}
难道不能通过 MultiBinding 设置 Rectangle.Width 吗?
【问题讨论】:
标签: wpf mvvm converter ivalueconverter