Multibinding within a list view

旧巷老猫 提交于 2021-02-10 05:48:20

问题


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>

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

   ...    
}

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)

holds, and the returned value is int_value , but still nothing shows up on the List View.

What's wrong?

All The Best, Dave


回答1:


A couple minutes ago i needed multibinding in a listview.

How i did it:

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




回答2:


DisplayMemberBinding is, as the name implies, not a value but a binding which points toward the member (property) which should be displayed. You can use the CellTemplate if you have complex ways of retrieving a value.

(To illustrate the above explanation: Your converter could return DisplayMemberBinding="42" which looks quite off since there is no binding or member to be found.)




回答3:


Just try

if (bool_value == true)
        return int_value.ToString();

It has worked when I reproduced your issue



来源:https://stackoverflow.com/questions/6046281/multibinding-within-a-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!