将CheckBox IsChecked值绑定到属性

时间:2016-09-12 12:52:00

标签: wpf xaml checkbox binding

我有一个ListView,其中每个项目都是CheckBox,后跟Name属性。

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

IsSelected3是继承INotifyPropertyChanged

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

问题是IsChecked绑定不起作用,而是名称的绑定。

你知道吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

两个变化:

Path=DataContext.IsSelected3
x:Type ListView

IsChecked绑定现在看起来像:

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

答案 1 :(得分:0)

感谢Kyle给出的想法,我找到了解决方案:

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

如果x:TypeListViewItem则有效。

感谢您的帮助!