这是我的问题,我有一个带复选框的列表视图,我想得到所选行的索引 我想得到行的索引以在验证后禁用此行
我尝试不同的方法
CheckBox cbx = sender as CheckBox.tag;
if (cbx != null) {
var index = cbx.Tag.ToString();
}
(((ContentPresenter)((CheckBox)sender).TemplatedParent)).IsEnabled = false; with this i disable just the checkbox
CheckBox cbx = sender as CheckBox.tag;
int index = (int)(sender as CheckBox).Tag;
答案 0 :(得分:5)
使用以下代码:
int index = YourListView.SelectedIndex;
答案 1 :(得分:3)
尝试以下示例代码。 添加如下属性
public int Selected
{
get { return _selected; }
set
{
_selected = value;
OnPropertyChanged(new PropertyChangedEventArgs("Selected"));
}
}
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
将 INotifyPropertyChanged 继承到您的cs文件中以获取 OnPropertyChanged
在视图中绑定listview
中的上述属性<Grid>
<ListView Margin="10" Name="lvUsers" SelectedIndex="{Binding Selected}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding ID}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}, Path=IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
尝试使用此属性检查复选框是否已禁用。我希望这对你有用。