列表视图中的多重绑定 [英] Multibinding within a list view
问题描述
我有一个包含几列的ListView.我将每个GridViewColumn绑定到一个属性都没有问题,例如:
I have a ListView that has a few columns. I have no problem binding each GridViewColumn to a property, For example:
<ListView ItemsSource="{Binding MyList}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}">
<GridViewColumnHeader Content="Name"/>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
当我尝试将GridViewColumn多绑定到两个属性时,问题开始了:
The problems start when I try to multibind a GridViewColumn to two properties:
<GridViewColumn>
<GridViewColumn.DisplayMemberBinding>
<MultiBinding Converter="{StaticResource DisplayMemberConverter}">
<Binding Path="HighestScore"/>
<Binding Path="IsHighestScoreApplicable"/>
</MultiBinding>
</GridViewColumn.DisplayMemberBinding>
<GridViewColumnHeader Content"Highest_Score"/>
我使用的转换器在这里给出:
The converter I use is given here:
public class DisplayMemberConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
int int_value = (int)values[0];
bool bool_value = (bool)values[1];
if (bool_value == true)
return int_value;
return null;
}
...
}
我想实现以下效果:'IsHighestScoreApplicable'属性为false时-列表视图中没有显示任何内容(空),为true时-显示了HighestScore值,但没有用-所有我即使"IsHighestScoreApplicable"为true,get也是一个空列.此外,当我调试Convert()方法时,我可以看到if语句:
I want to achieve the effect that when 'IsHighestScoreApplicable' property is false - nothing is shown on the list view (empty), and when it's true - the HighestScore value is shown, but that doesn't work out - all I get is an empty column even when ''IsHighestScoreApplicable' is true. Furthermore, when I debug Convert() method I can see that the if statement:
if (bool_value == true)
保持不变,返回的值是int_value,但列表视图上仍然没有任何显示.
holds, and the returned value is int_value , but still nothing shows up on the List View.
怎么了?
万事如意,戴夫
推荐答案
几分钟前,我需要在列表视图中进行多重绑定.
A couple minutes ago i needed multibinding in a listview.
我是怎么做到的:
我为该单元创建了一个数据模板.数据模板包含一个文本块,我们在该文本块上进行多重绑定.
I made a datatemplate for the cell. The datatemplate contains an textblock, on that textblock we doing multibinding.
<ListView Margin="33,0,0,0" ItemsSource="{Binding HourRegistry}">
<ListView.View>
<GridView>
<GridViewColumn Width="200" Header="Worker" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Employee.FirstName" />
<Binding Path="Employee.Name" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="100" Header="Job" DisplayMemberBinding="{Binding Path=Keycode}"/>
</GridView>
</ListView.View>
也许晚了,但对其他人来说很方便
Maybe late but handy for other people
这篇关于列表视图中的多重绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!