我有一个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
绑定不起作用,而是名称的绑定。
答案 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:Type
为ListViewItem
则有效。
感谢您的帮助!