如何与listview绑定

时间:2016-08-21 17:06:34

标签: c# wpf listview binding

如何与listview绑定?我上了课,我收集了我,但是当我做约束时,它没有工作:

        public class DrawingVisualObject : DrawingVisual
       {
        public int Id { get; set; }
        public string Name { get; set; }
        public DrawingVisualObject(int id, string name)
        {
            Id = id;
            Name = name;
        }
    }

    ObservableCollection<DrawingVisualObject> mPointNames = new     ObservableCollection<DrawingVisualObject>();
    public MainWindow()
    {
        this.DataContext = mPointNames;
        InitializeComponent();
        lstv.ItemsSource = mPointNames;
    }

    public ObservableCollection<DrawingVisualObject> PointNames
    {
        get
        {
            return mPointNames;
        }
    }

这是我写的xaml代码

<ListView MinHeight="350" ItemsSource="{Binding DrawingVisualObject, Mode=TwoWay}" Name="lstv">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Id}"/>

2 个答案:

答案 0 :(得分:1)

您不必使用DrawingVisual进行绑定。在您的ViewModel或您的Model ceate类FooItem

public class FooItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public FooItem(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

然后在ViewModel中创建ObservableCollection

 public ObservableCollection<FooItem> PointNames
    {
        get
        {
            return mPointNames;
        }
    }

在您的xaml使用中:

<ListView Margin="10" Name="FooItems" ItemsSource="{Binding Path=PointNames}">
        <ListView.View>
                <GridView>
                        <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                        <GridViewColumn Header="Id" Width="50" DisplayMemberBinding="{Binding Id}" />
                </GridView>
        </ListView.View>
</ListView>

删除“lstv.ItemsSource = mPointNames;”来自View构造函数。在MVVM中,应该只有init调用,最大事件处理程序初始化。

希望有所帮助

答案 1 :(得分:0)

你的例子看起来不像MVVM,但如果你想在你的代码中有数据 - 用依赖属性来做。

以下是XAML的示例

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Test"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    Name="myWindow">
<Grid>
          <ListView ItemsSource="{Binding ElementName=myWindow, Path=DrawingVisualCollection}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"></GridViewColumn>
                <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Path=Id}"></GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

</Grid>

背后的代码看起来像这样

namespace Test
{

public class DrawingVisualObject : DrawingVisual
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DrawingVisualObject(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    public ObservableCollection<DrawingVisualObject> DrawingVisualCollection
    {
        get { return (ObservableCollection<DrawingVisualObject>)GetValue(DrawingVisualCollectionProperty); }
        set { SetValue(DrawingVisualCollectionProperty, value); }
    }

    // Using a DependencyProperty as the backing store for DrawingVisualCollection.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DrawingVisualCollectionProperty =
        DependencyProperty.Register("DrawingVisualCollection", typeof(ObservableCollection<DrawingVisualObject>), typeof(MainWindow), new PropertyMetadata(new ObservableCollection<DrawingVisualObject>()));

    public MainWindow()
    {
        InitializeComponent();

        List<DrawingVisualObject> sample = new List<DrawingVisualObject>();

        sample.Add(new DrawingVisualObject(1, "Yolo"));
        sample.Add(new DrawingVisualObject(2, "Swag"));

        this.FillCollection(sample);

    }

    public void FillCollection(IEnumerable<DrawingVisualObject> objects2fill)
    {
        this.DrawingVisualCollection.Clear();

        foreach(DrawingVisualObject obj in objects2fill)
        {
            this.DrawingVisualCollection.Add(obj);
        }
    }


}

}

这应该对你有用

请记住: 如果你再次初始化Collection,你可能会破坏绑定。我试图在示例方法FillCollection()中显示它。所以这意味着如果你使用MVVM,你应该总是将你的ObservableCollection设置为私有集。

我建议你更准确地再次访问MVVM。要知道viewmodels的共同基础是好的start !!!