Binding ListView DataTemplate ListView

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>

Elements in a ListView are of the type below:

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 tied to the IsChecked value from the ListViewItem. The converter is a simple visibility 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 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;
        }
    }

Linking the visibility of a checkbox for me does not work. The default value of IsChecked is false, so the list is displayed without flags. If I select an item, this check box will not appear.

However, if I set the default IsChecked value to true, all the items in the list will appear with a check box, and if I select an item and then uncheck it, the check box will disappear correctly.

, , , , , .

, ?

+3
2

GridViewColumn . , ListView , , .

, BoolToVisibilityConverter, Visibility.Hidden Visibility.Collapsed.

+1

, , , :

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

DataTemplate, MyItem:

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

Source: https://habr.com/ru/post/1771364/


All Articles