在WPF中将类绑定到gridview的问题

时间:2013-10-24 08:55:20

标签: wpf listview gridview binding

我正在尝试将自定义类的observablecollection绑定到一个简单的gridview。

这是gridview模板:

<ListView Name="TestGrid" Grid.Row="1" Margin="0,10,0,0" ItemsSource="{Binding Path=testlist}">
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=AlertType}" Header="Alert" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Target}" Header="Target" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Message}" Header="Message" />
        </GridView>
    </ListView>

这是背后的代码:

public ObservableCollection<TestClass> testlist {get; set;}

public MainWindow()
 {
        InitializeComponent();

        testlist = new ObservableCollection<TestClass>();
        testlist.Add(new TestClass("Type1","Target1","Message1"));
        testlist.Add(new TestClass("Type2", "Target2", "Message2"));
 }

public class TestClass
{
    public string AlertType { get; set; }
    public string Target { get; set; }
    public string Message { get; set; }

    public TestClass(string a, string t, string m)
    {
        AlertType = a;
        Target = t;
        Message = m;
    }
}

现在我一直在关注我能在互联网上找到的样本。不幸的是,我在gridview中得到的唯一结果是显示以下消息的唯一行:“System.Windows.Controls.Columns.Count:3”

我在这里缺少什么?

3 个答案:

答案 0 :(得分:2)

将您的xaml和代码更新为:

          <ListView Name="TestGrid" Grid.Row="1" Margin="0,10,0,0" ItemsSource="{Binding Path=testlist}">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=AlertType}" Header="Alert" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Target}" Header="Target" />
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Message}" Header="Message" />
                </GridView>
            </ListView.View>
        </ListView>

背后的代码

public MainWindow()
{
    InitializeComponent();

    testlist = new ObservableCollection<TestClass>();
    testlist.Add(new TestClass("Type1","Target1","Message1"));
    testlist.Add(new TestClass("Type2", "Target2", "Message2"));
    DataContext = this;

}

答案 1 :(得分:2)

你错过了ListView上的View属性:

<ListView Name="TestGrid" Grid.Row="1" Margin="0,10,0,0" ItemsSource="{Binding Path=testlist}">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding Path=AlertType}" Header="Alert" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Target}" Header="Target" />
            <GridViewColumn DisplayMemberBinding="{Binding Path=Message}" Header="Message" />
        </GridView>
    </ListView.View>    
</ListView>

答案 2 :(得分:0)

您可以尝试为窗口创建viewModel并将其设置为窗口的datacontext。 更新您的代码:

 public partial class MainWindow : Window
{
    private ExampleViewModel m_ViewModel;      
    public MainWindow()
    {
        InitializeComponent();
        m_ViewModel = new ExampleViewModel();
        DataContext = m_ViewModel;
    }
}


public class ExampleViewModel : INotifyPropertyChanged
{
    private ObservableCollection<TestClass> m_testlist;
    public ExampleViewModel()
    {
        m_testlist = new ObservableCollection<TestClass>();
        m_testlist.Add(new TestClass("Type1", "Target1", "Message1"));
        m_testlist.Add(new TestClass("Type2", "Target2", "Message2"));
    }

    public ObservableCollection<TestClass> testlist
    {
        get
        {
            return m_testlist;
        }
        set
        {
            if (m_testlist != value)
            {
                m_testlist = value;
                OnPropertyChanged("testlist");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}

public class TestClass
{
    public string AlertType { get; set; }
    public string Target { get; set; }
    public string Message { get; set; }

    public TestClass(string a, string t, string m)
    {
        AlertType = a;
        Target = t;
        Message = m;
    }
}