2

here is my problem , i have a listview with checkbox , and i want to get the index of the row which is selected i want to get the index of the row to disable this row after validation

i try different method

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;
2
  • It would be helpful if you shared your ListView code with us, too. Also, can you select one single row? Or multiple ones? Commented Jun 18, 2015 at 12:21
  • you can select multiple ones Commented Jun 18, 2015 at 12:43

2 Answers 2

5

Use Below Code For One Row:

int index = YourListView.SelectedIndex;
Sign up to request clarification or add additional context in comments.

2 Comments

error with selected "selected can only appear on the left hand side of += ..."
I thought your question refers to WinForms. I corrected it. now you can use above code which works in WPF.
4

Try below sample code. Add a property as below

public int Selected
        {
            get { return _selected; }

            set
            {
                _selected = value;

                OnPropertyChanged(new PropertyChangedEventArgs("Selected"));
            }
        }

        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }

Inherit INotifyPropertyChanged into your cs file to get OnPropertyChanged

In the view bind the above property in 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>

Try checking if the checkbox is disabled or not with this property. I hope this will works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.