【问题标题】:listview with checkbox get selected row index带有复选框的列表视图获取选定的行索引
【发布时间】:2015-09-04 00:37:32
【问题描述】:

这是我的问题,我有一个带有复选框的列表视图,我想获取所选行的索引 我想在验证后获取该行的索引以禁用该行

我尝试不同的方法

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;

【问题讨论】:

  • 如果您也与我们分享您的 ListView 代码将会很有帮助。另外,您可以选择单行吗?还是多个?
  • 可以多选

标签: c# wpf listview checkbox


【解决方案1】:

在一行中使用下面的代码:

int index = YourListView.SelectedIndex;

【讨论】:

  • error with selected "selected can only appear on the left hand side of += ..."
  • 我认为你的问题是指 WinForms。我纠正了它。现在你可以使用上面在 WPF 中工作的代码了。
【解决方案2】:

试试下面的示例代码。 添加如下属性

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>

尝试使用此属性检查复选框是否被禁用。我希望这对你有用。

【讨论】:

    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2012-03-09
    • 2016-03-30
    相关资源
    最近更新 更多