Home > front end >  how to get listview item and pass it to a method in wpf c#
how to get listview item and pass it to a method in wpf c#

Time:10-19

i have an xaml file where a listview is defined. corresponding xaml.cs file, i have two methods CheckBox_Checked and CheckBox_UnChecked. the checkbox is just another column in the line. my question is, how can i pass the listview line as a paramater in CheckBox_Checked method ?

intention is to know which line was checked. Issue is multiple checkbox can be checked,so, i need to know exactly which checkbox was checked.

ListView as binding to a list of a custom class

each gridviewcolumn is variablees in the custom class enter image description here

CodePudding user response:

You could cast the DataContext of the sender argument to your data type in the event handler:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    CheckBox checkBox = (CheckBox)sender;
    YourCustomClass dataObject = checkBox.DataContext as YourCustomClass;
    if (dataObject != null)
    {
        //access the xxx and yyy properties of YourCustomClass here...
    }
}

CodePudding user response:

I'll simply explain how to tell which checkbox is selected in the code-behind.

XAML

<ListView x:Name="ListView02" Grid.Column="1">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Check">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox Checked="CheckBox_Checked" IsChecked="{Binding IsChecked, Mode=TwoWay}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" />
            <GridViewColumn DisplayMemberBinding="{Binding Type}" Header="Type" />
            <GridViewColumn Header="Image">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Border Background="Gray">
                            <Image
                                Width="32"
                                Height="32"
                                Source="{Binding ImagePath}" />
                        </Border>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

Code

public partial class ListView1 : Window
{
    private List<Animal> _list;

    public ListView1()
    {
        InitializeComponent();

        _list = new List<Animal>
        {
            new Animal { IsChecked=true, Name = "Cat",  Type = "animal", ImagePath = @"Images\cat.png"},
            new Animal { IsChecked=false, Name = "Dog",  Type = "animal", ImagePath = @"Images\dog.png"},
            new Animal { IsChecked=true, Name = "Fish",  Type = "fish", ImagePath = @"Images\fish.png"},
            new Animal { IsChecked=false, Name = "Flower",  Type = "plant", ImagePath = @"Images\flower.jpg"},
        };
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        ListView01.ItemsSource = _list;
        ListView02.ItemsSource = _list;
        ListBox.ItemsSource = _list;
        DataGrid.ItemsSource = _list;
    }

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        //You can check the data in this part.
        var data = ((CheckBox)sender).DataContext as Animal;
        if(data == null)
        {
            return;
        }
        Debug.WriteLine($"{data.Name} {data.Type} {data.ImagePath}");
    }
}
  • Related