将DataGrid绑定到另一个对象

时间:2016-07-12 22:57:52

标签: c# wpf datagrid

我有一个包含可观察字符串集合的对象,如何绑定数据网格以显示这些字符串?

以例如:

public class Container
{
    public ObservableCollection<string> strs; //This won't work, see edit!
.
.
.
}

XAML:

<DataGrid  ItemsSource="{Binding Container}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Strings" Binding="{Binding}" />
    </DataGrid.Columns>
</Datagrid>

为搜索者编辑:上述方法存在一些问题,首先您可以通过简单地引用这些属性来绑定项目的属性。在这种情况下:

ItemsSource="{Binding Container.strs}"

其次,字符串的内容不是字符串的属性,所以

Binding="{Binding}"

直接绑定到字符串,而不是试图找到它的属性(例如长度)

最后你不能绑定到字段,只有属性,有什么区别?

public ObservableCollection<string> strs; //This is a field
public ObservableCollection<string> strs {get; set;} //This is  property

Bonus:如果您只是实例化strs一次,那么ObservableCollection将通知它在更改为/内时绑定的任何内容,但如果您要更改指针则不会,要解决此问题,您可以使用依赖属性!

在视觉工作室中,最好使用内置代码段,因为有很多内容需要填写:&#39; propdp&#39;并点击两次标签,在这种情况下,我们有:

public ObservableCollection<string> strs
    {
        get { return (ObservableCollection<string>)GetValue(strsProperty); }
        set { SetValue(strsProperty, value); }
    }

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

1 个答案:

答案 0 :(得分:1)

这对我有用。

XAML:

<Window x:Class="WpfApplication2.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:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <DataGrid ItemsSource="{Binding thing.stuff}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="Auto" Header="String Contents" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

和后面的代码(C#):

using System.Windows;
using System.Collections.ObjectModel;

namespace WpfApplication2
{
    public class Thing
    {
        // make sure this is a property, not a field.
        // furthermore, make sure it is public. 
        public ObservableCollection<string> stuff
        {
            get; set;
        }

        public Thing()
        {
            stuff = new ObservableCollection<string>();
            stuff.Add("A String");
            stuff.Add("Another String");
            stuff.Add("Yet Another String");
        }
    }

    public partial class MainWindow : Window
    {
        public Thing thing{get;set;}
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            thing = new Thing();
        }
    }
}

我建议你更多地充实你的问题。请记住,StackOverflow的目标是问题对其他用户以及您自己都有帮助。

编辑:Terser XAML

<Window x:Class="WpfApplication2.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:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <DataGrid ItemsSource="{Binding thing.stuff}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Width="Auto" Binding="{Binding}" Header="String Contents" />
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>