C# 三列GridView:一个只包含文本,另两个包含复选框

C# 三列GridView:一个只包含文本,另两个包含复选框,c#,.net,wpf,data-binding,C#,.net,Wpf,Data Binding,我无法将值绑定到复选框,随后将复选框绑定到GridViewColumn。基本上,我有三列分别命名为Property、Tele和Surr,我有一个Row类型的对象列表。行有三个属性:Name(string)、Tel(bool)和Sur(bool)。我想用所有的名称填充属性(通过DisplayMemberBinding,这似乎工作正常),另外两列带有复选框,其状态对应于Tel和Sur的值 我哪里做错了 XAML: <UserControl x:Class="WpfApplication2.Ma

我无法将值绑定到复选框,随后将复选框绑定到GridViewColumn。基本上,我有三列分别命名为Property、Tele和Surr,我有一个Row类型的对象列表。行有三个属性:Name(string)、Tel(bool)和Sur(bool)。我想用所有的名称填充属性(通过DisplayMemberBinding,这似乎工作正常),另外两列带有复选框,其状态对应于Tel和Sur的值

我哪里做错了

XAML:

<UserControl x:Class="WpfApplication2.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         DataContext="{Binding RelativeSource={RelativeSource Self}}"
         Margin="0,0,-8,0">

<Grid x:Name="GView" Margin="0,0,-8,0">
    <TextBox HorizontalAlignment="Left" Height="23" Margin="114.99,12.96,0,0" TextWrapping="Wrap" Text="DefaultName" VerticalAlignment="Top" Width="175.01"/>
    <Label Content="Name:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>

    <ListView DataContext="{Binding ElementName=rows}" 
              x:Name="LView" HorizontalAlignment="Left" Height="159" Margin="0,52,0,0" VerticalAlignment="Top" Width="292">

        <ListView.View>
            <GridView>
                <GridViewColumn x:Name="Property" 
                                Width="100" 
                                Header="Property"
                                DisplayMemberBinding="{Binding Name}">
                </GridViewColumn>
                <GridViewColumn x:Name="Tele" 
                                Width="100" 
                                Header="In Tele?"
                                DisplayMemberBinding="{Binding Tel}">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="telCheck" 
                                      IsChecked="{Binding Source={RelativeSource Tel}}"></CheckBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn x:Name="Surr" 
                                Width="100" 
                                Header="In Surr?" 
                                DisplayMemberBinding="{Binding Sur}">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="telCheck" 
                                      IsChecked="{Binding Source={RelativeSource Sur}}"></CheckBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>
public partial class MainWindow : UserControl
{
    private ObservableCollection<Row> rows; 

    public ObservableCollection<Row> Rows
    {
        get { return rows; }
    }

    public MainWindow()
    {
        rows = new ObservableCollection<Row>();
        rows.Add(new Row("item1", true, false));
        rows.Add(new Row("item2", true, true));
        InitializeComponent();
    }
}
public class Row : INotifyPropertyChanged
{
    private string _name;
    private bool _tel;
    private bool _sur;

    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value)
                return;
            _name = value;
            NotifyPropertyChanged("Name");
        }
    }
    public bool Tel
    {
        get { return _tel; }
        set
        {
            if (_tel == value)
                return;
            _tel = value;
            NotifyPropertyChanged("Tel");
        }
    }
    public bool Sur
    {
        get { return _sur; }
        set
        {
            if (_sur == value)
                return;
            _sur = value;
            NotifyPropertyChanged("Sur");
        }
    }

    public Row(string name, bool value, bool value2)
    {
        Name = name;
        Tel = value;
        Sur = value2;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

}

}对代码进行了一些更改并使其正常工作

首先,不要在窗口构造函数中填充行集合,请在加载窗口后进行。不幸的是,我无法将集合绑定到xaml中的网格
ItemsSource
,因此我在填充集合后在codebehind中进行了绑定。并使用
DataContext=“{Binding ElementName=rows}”
不正确,因为
集合是
私有的
,您需要
puplic
集合名为

还有一件事-要使复选框显示为复选框而不是文本:

  • 从Tele adn Surr列定义中删除
    DisplayMemberBinding
  • 不要使用
    IsChecked=“{Binding Source={RelativeSource Tel}}
    而使用
    IsChecked=“{Binding Tel}
    ,对于Sur

  • 不确定为什么要使用DisplayMemberBinding。请尝试删除DisplayMemberBinding,并将IsChecked=“{Binding Source={RelativeSource Tel}}等更改为IsChecked=“{Binding Path=Tel}”等等。一旦你为你的集合设置了DataContext,你应该只需要在模板上设置一个简单的绑定路径。等等,它不再起作用了…我目前拥有的是我通常在代码隐藏中设置DataContext…在XAML中我将设置ItemsSource=“{binding}”,在代码隐藏中设置LView.DataContext=行(不一定在构造函数中,但始终按时间顺序在InitializeComponent之后)