首页 > 解决方案 > 在 C# ListView Checkbox 中使用数据绑定获取 CheckBox 内容

问题描述

来自 python,需要将我制作的一些旧应用程序翻译成 C#。我有一个带有复选框的 ListView。XAML

<ListView Name ="spored" SelectionMode="Multiple" HorizontalAlignment="Left" VerticalAlignment="Stretch" Margin="10,10,10,10" Width="750">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="data">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Content="{Binding Name}" IsChecked="{Binding CheckedShow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
           </GridView>
        </ListView.View>

在代码中,我拥有填充此 Listview 所需的一切,并且效果很好。

    public class Show 
        {
            public bool CheckedShow { get; set; } //listview checkbox

            public string Name { get; set; } //listview checkbox content
        }

public List<Show> showList = new List<Show>();

private void ShowSpored_Click(object sender, RoutedEventArgs e)
{
        //here i have some foreeach loop that populates showList
        foreach(...)
        {
        .
        .
        .
        showList.Add(new Show() { CheckedShow = false, Name = infoKey });
        }
    
//after loop i populate Listview with
spored.ItemsSource = showList;
}

现在我需要实现并且不知道以下是如何实现的。填充列表时,我需要在检查的内容上运行一些代码。我想我需要用 INotifyPropertyChanged.PropertyChanged 事件来做到这一点,但对于我这个菜鸟,我似乎无法设法获得检查的内容......

public class Content : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
.
.
.
}

请帮忙!!!

标签: c#wpflistviewcheckbox

解决方案


弄清楚了。必须将此添加到cs:

private void OnPropertyChanged(string property)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(property));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

然后创建一个这样的类:

public class Show: INotifyPropertyChanged
        {
            private bool _IsChecked;
            private string _Name;


            public bool IsChecked
            {
                get { return _IsChecked; }

                set
                {
                    if (_IsChecked != value)
                    {
                        _IsChecked = value;
                        OnPropertyChanged("IsChecked");

                        if (_IsChecked == true)
                        {
                            Console.WriteLine(_Name + "do some shit");
                        }
                        else
                        { Console.WriteLine("do some other shit"); }

                    }
                }
            }

            public string Name
            {
                get
                {
                    return _Name;
                }
                set
                {
                    if (_Name != value)
                    {
                        _Name = value;
                    }

                }
            }

推荐阅读