Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 绑定到Listview和复选框_C#_Wpf - Fatal编程技术网

C# 绑定到Listview和复选框

C# 绑定到Listview和复选框,c#,wpf,C#,Wpf,我有以下XAML代码: <ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Path=GROUP_NAME}"

我有以下XAML代码:

<ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock  Text="{Binding Path=GROUP_NAME}" />
                <CheckBox IsChecked="{Binding GroupIsChecked}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListView>


“组”是我的数据库中的表,它包含
GROUP\u NAME
列,但不包含
GroupIsChecked
。到目前为止,我设法显示TextBlock,但我不知道如何将复选框设置为true/false。问题是
GroupIsChecked
不在我的表中,我想检查一些条件以将其设置为true/false。有什么想法吗?

MVVM的主要思想是有一个“中产阶级”将您的模型与您的视图联系起来

在您的情况下,模型类是数据库表对象,视图是UI

您需要编写一个ViewModel类,该类表示表中的一行,在那里您可以编写任何需要的属性。 这些视图模型的集合将成为您的“中间”表。 将此集合绑定到ListView

GroupIsChecked属性不应在表中,因此可以在ViewModel类中实现其逻辑。
也许您可以为表中的所有列指定更合适的组名,如GroupName等。

MVVM的主要思想是有一个“中间”类将模型链接到视图

在您的情况下,模型类是数据库表对象,视图是UI

您需要编写一个ViewModel类,该类表示表中的一行,在那里您可以编写任何需要的属性。 这些视图模型的集合将成为您的“中间”表。 将此集合绑定到ListView

GroupIsChecked属性不应在表中,因此可以在ViewModel类中实现其逻辑。
也许您可以为表中的所有列指定更合适的组名称,如GroupName等。

OrMiz关于为视图实现视图模型的说法是正确的

我不是MVVM专家,但这里有一个例子可以说明如何做到这一点。 欢迎反馈

表示组表的类

public class GroupTable
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
}
可以表示列表视图中一行的视图模型玻璃

public class GroupViewModel : INotifyPropertyChanged
{
    public GroupViewModel(GroupTable group)
    {
        this.GroupName = group.Name;
        this.GroupIsChecked = GroupCheckedLogic();
    }

    private string _GroupName;

    public string GroupName
    {
        get { return _GroupName; }
        set
        {
            _GroupName = value;
            OnPropertyChanged();
        }
    }

    private bool _GroupIsChecked;

    public bool GroupIsChecked
    {
        get { return _GroupIsChecked; }
        set
        {
            _GroupIsChecked = value;
            OnPropertyChanged();
        }
    }

    private bool GroupCheckedLogic()
    {
        // Your logic goes here
        return true;
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}
然后是视图的主视图模型

public class GroupListViewModel : INotifyPropertyChanged
{
    public GroupListViewModel()
    {
        // Get your actual data
        var groupsData = new List<GroupViewModel>
        {
            new GroupViewModel(new GroupTable {Id = 1, Name = "Group 1"}),
            new GroupViewModel(new GroupTable {Id = 2, Name = "Group 2"})
        };

        this.Groups = new ObservableCollection<GroupViewModel>(groupsData);
    }

    private ObservableCollection<GroupViewModel> _Groups;

    public ObservableCollection<GroupViewModel> Groups
    {
        get { return _Groups; }
        set
        {
            _Groups = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
公共类GroupListViewModel:INotifyPropertyChanged
{
公共GroupListViewModel()
{
//获取您的实际数据
var groupsData=新列表
{
新的GroupViewModel(新的GroupTable{Id=1,Name=“Group 1”}),
新的GroupViewModel(新的GroupTable{Id=2,Name=“Group 2”})
};
this.Groups=新的ObservableCollection(groupsData);
}
私人可观察收集组;
公共收集组
{
获取{return\u Groups;}
设置
{
_组=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
this.PropertyChanged?.Invoke(this,newpropertychangedeventargs(propertyName));
}
}
然后在你的XAML中

<ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding GroupName}" />
                    <CheckBox IsChecked="{Binding GroupIsChecked}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListView>

OrMiz为您的视图实现视图模型是正确的

我不是MVVM专家,但这里有一个例子可以说明如何做到这一点。 欢迎反馈

表示组表的类

public class GroupTable
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }
}
可以表示列表视图中一行的视图模型玻璃

public class GroupViewModel : INotifyPropertyChanged
{
    public GroupViewModel(GroupTable group)
    {
        this.GroupName = group.Name;
        this.GroupIsChecked = GroupCheckedLogic();
    }

    private string _GroupName;

    public string GroupName
    {
        get { return _GroupName; }
        set
        {
            _GroupName = value;
            OnPropertyChanged();
        }
    }

    private bool _GroupIsChecked;

    public bool GroupIsChecked
    {
        get { return _GroupIsChecked; }
        set
        {
            _GroupIsChecked = value;
            OnPropertyChanged();
        }
    }

    private bool GroupCheckedLogic()
    {
        // Your logic goes here
        return true;
    }


    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    }
}
然后是视图的主视图模型

public class GroupListViewModel : INotifyPropertyChanged
{
    public GroupListViewModel()
    {
        // Get your actual data
        var groupsData = new List<GroupViewModel>
        {
            new GroupViewModel(new GroupTable {Id = 1, Name = "Group 1"}),
            new GroupViewModel(new GroupTable {Id = 2, Name = "Group 2"})
        };

        this.Groups = new ObservableCollection<GroupViewModel>(groupsData);
    }

    private ObservableCollection<GroupViewModel> _Groups;

    public ObservableCollection<GroupViewModel> Groups
    {
        get { return _Groups; }
        set
        {
            _Groups = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
公共类GroupListViewModel:INotifyPropertyChanged
{
公共GroupListViewModel()
{
//获取您的实际数据
var groupsData=新列表
{
新的GroupViewModel(新的GroupTable{Id=1,Name=“Group 1”}),
新的GroupViewModel(新的GroupTable{Id=2,Name=“Group 2”})
};
this.Groups=新的ObservableCollection(groupsData);
}
私人可观察收集组;
公共收集组
{
获取{return\u Groups;}
设置
{
_组=值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
this.PropertyChanged?.Invoke(this,newpropertychangedeventargs(propertyName));
}
}
然后在你的XAML中

<ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding GroupName}" />
                    <CheckBox IsChecked="{Binding GroupIsChecked}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListView>


对象的表或C#集合吗?我希望你用MVVM的方式来做。在这种情况下,可以在
类中检查属性
IsChecked
属性。
对象的表或集合吗?我希望你用MVVM的方式来做。在这种情况下,您可以在
类中检查属性
属性。这就是我的意思:)解决我的问题,谢谢你们的解释这就是我的意思:)解决我的问题,谢谢你们的解释