开发者

ListView DataTemplate binding

开发者 https://www.devze.com 2023-01-21 17:51 出处:网络
I have the following ListView: <ListView Name=\"listView\"> <ListView.View> <GridView>

I have the following ListView:

    <ListView Name="listView">
            <ListView.View>
                <GridView>
                    <GridView.ColumnHeaderContainerStyle>
                        <Style TargetType="{x:Type GridViewColumnHeader}">
                            <Setter Property="Visibility"
                                    Value="Collapsed"/>
                        </Style>
                    </GridView.ColumnHeaderContainerStyle>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <CheckBox
                                          Margin="0"
                                          VerticalAlignment="Center"
                                          IsChecked="{Binding IsChecked}"
                                          Visibility="{Binding IsChecked, Converter={StaticResource boolToVis}}">
                                    </CheckBox>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="0"
                                           Text="{Binding Text}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

The items in the ListView are of the below type:

public class CheckBoxListViewItemSource : INotifyPropertyChanged
{
    public CheckBoxListViewItemSource(String text)
    {
        m_text = text;
    }

    public bool IsChecked
    {
        get { return m_checked; }
        set
        {
            if (m_checked == value) return;
            m_checked = value;
            RaisePropertyChanged("IsChecked");
        }
    }

    public String Text
    {
        get { return m_text; }
        set
        {
            if (m_text == value) return;
            m_text = value;
            RaisePropertyChanged("Text");
        }
    }

    public override string ToString()
    {
        return Text;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propName)
    {
        PropertyChangedEventHandler eh = PropertyChanged;
        if (eh != null)
        {
            eh(this, new PropertyChangedEventArgs(propName));
        }
    }

    private bool m_checked;
    private String m_text;
}

The visibility of the checkbox in the ListView is bound to the value of IsChecked of the ListViewItem. The converter is a simple bool to visibility converter:

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                          CultureInfo culture)
    {
        if (value is Boolean)
        {
            return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

In the code behind of the ListView I have:

    void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        foreach (var item in e.RemovedItems)
        {
            CheckBoxListViewItemSource source = item as CheckBoxListViewItemSource;
            source.IsChecked = false;
        }
        foreach (var item in e.AddedItems)
        {
            CheckBoxListViewItemSource source = item as CheckBoxListViewItemSource;
            source.IsChecked = true;
        }
    }

The binding for the checkbox visibility is not working for me. The default IsChecked value is false so the list appears with no checkboxes. If I select an item, the checkbox does not appear.

However, if I set the default value of IsChecked to true, all of the list items appear with a checkbox and if I select an item and then deselect it, the checkbox correctly disappears.

What I am trying to achieve is that all of the items start off with no checkbox, selecting an item displays a checked checkbox, and deselecting an item hides the checkbo开发者_高级运维x.

Any ideas where I am going wrong?


Manually set width of the first GridViewColumn to a fixed value. It seems ListView sets width of it to zero if it contains nothing and doesn't update width when checkboxes start to appear.

Alternatively, change code of BoolToVisibilityConverter to return Visibility.Hidden instead of Visibility.Collapsed.


I know this has been answered but I got around this problem using the following:

 <GridViewColumn Header="MyColumn">
       <GridViewColumn.CellTemplate>
              <DataTemplate>
                    <ContentPresenter Content="{Binding MyItem, UpdateSourceTrigger=PropertyChanged}" ContentTemplate="{StaticResource myTemplate}"/>
              </DataTemplate>
         </GridViewColumn.CellTemplate>
 </GridViewColumn>

And in the Window I had a DataTemplate defined for the type that MyItem was:

<Window.Resources>
        <DataTemplate DataType="{x:Type myViewModels:MyItemViewModel}" x:Key="myTemplate" >
           ...template code
        </DataTemplate>
</Window.Resources>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号