1

我正在尝试将自定义类的 observablecollection 绑定到简单的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”

我在这里想念什么?

4

3 回答 3

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>
于 2013-10-24T09:07:45.043 回答
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;

}

于 2013-10-24T09:04:59.657 回答
0

您可以尝试为窗口创建 viewModel 并将其设置为窗口的数据上下文。更新您的代码:

 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;
    }
}
于 2013-10-24T09:17:33.073 回答