ctt_it
ctt_it

Reputation: 339

Binding CheckBox IsChecked value to a Property

I have a ListView where each Item is a CheckBox followed by the Nameproperty.

  <ListView Name="ShapesList"
           SelectionChanged="ShapesList_OnSelectionChanged"
           Grid.Row="2" Grid.Column="0"
           ItemsSource="{Binding ChartViewModel.OidList}"
           Margin="15,0,10,10"
           SelectionMode="Multiple">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn>
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Style="{DynamicResource BigCheckbox}"
                                        IsChecked="{Binding Path=IsSelected3, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Mode=TwoWay}"
                                        Content="{Binding Path=Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"
                                         Checked="RoiChecked" Unchecked="RoiUnchecked"/>
                         </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="ROI" DisplayMemberBinding="{Binding Name}"  />
            </GridView.Columns>
        </GridView>
    </ListView.View>

Where IsSelected3 is a Property of a Class that inherits INotifyPropertyChanged.

 public bool IsSelected3
 {
     get { return _isSelected3; }
     set
     {
         _isSelected3 = value;
         FirePropertyChanged("IsSelected3");
     }
  }

The problem is that the IsChecked binding doesn't work, instead the one with Name does.

Do you have any idea? Thanks!

Upvotes: 0

Views: 1145

Answers (2)

ctt_it
ctt_it

Reputation: 339

Thanks to the idea given by Kyle I found the solution:

IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Path=DataContext.IsChecked, Mode=TwoWay}"

If the x:Type is ListViewItem it works.

Thanks for the help!

Upvotes: 1

Kyle Hancock
Kyle Hancock

Reputation: 122

Two changes:

Path=DataContext.IsSelected3
x:Type ListView

IsChecked binding now looks like:

IsChecked="{Binding Path=DataContext.IsSelected3, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Mode=TwoWay}"

Upvotes: 0

Related Questions