0

我无法将值绑定到复选框,然后将复选框绑定到 GridViewColumn。基本上,我有三列分别命名为 Property、Tele 和 Surr,并且我有一个 Row 类型的对象列表。行具有三个属性:名称(字符串)、电话(布尔)和苏尔(布尔)。我想用所有名称填充属性(这似乎工作正常,通过 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();
    }
}

}

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

}

}

4

1 回答 1

0

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

首先,不要在窗口构造函数中填充行集合,在加载窗口后执行。不幸的是,我无法将集合绑定到ItemsSourcexaml 中的网格,所以我在填充集合后在代码隐藏中完成了它。并且使用DataContext="{Binding ElementName=rows}"不正确只是因为rowscollection 是private,你需要pupliccollection named Rows

还有一件事 - 让您的复选框显示为复选框而不是文本:

  1. DisplayMemberBinding从 Tele 和 Surr 列定义中删除
  2. 而不是IsChecked="{Binding Source={RelativeSource Tel}}使用IsChecked="{Binding Tel}和相同的 Sur
于 2013-06-21T07:21:56.660 回答