三列GridView:一个只有文本,另外两个有复选框

时间:2013-06-20 21:19:52

标签: c# .net wpf data-binding

我无法将值绑定到复选框,随后是GridViewColumn的复选框。基本上,我有三列分别名为Property,Tele和Surr,我有一个Row类型的对象列表。 Row有三个属性:Name(字符串),Tel(bool)和Sur(bool。我想用所有名称填充Property(并且看起来工作正常,通过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>

XAML.CS:

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();
    }
}

}

Row.cs:

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));
        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

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

首先,不要在窗口构造函数中填充行集合,在加载窗口后执行此操作。不幸的是我无法将集合绑定到xaml中的网格ItemsSource,所以我在填充集合后在代码隐藏中完成了它。由于DataContext="{Binding ElementName=rows}"集合为rows,因此使用private不正确,您需要puplic名为Rows的集合。

另一件事 - 让复选框显示为复选框而不是文本:

  1. 从Tele adn Surr列定义中删除DisplayMemberBinding
  2. 而不是IsChecked="{Binding Source={RelativeSource Tel}}使用IsChecked="{Binding Tel}和Sur
  3. 相同