【问题标题】:Bind DataGrid to ObservableCollection of strings within another object将 DataGrid 绑定到另一个对象中字符串的 ObservableCollection
【发布时间】:2016-07-12 22:57:52
【问题描述】:

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

举个例子:

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

奖励:如果您只是实例化 strs 一次,那么 ObservableCollection 将在更改到/在其中发生时通知其绑定的任何内容,但如果您更改指针,则不会,修复这你可以使用依赖属性!

在visual studio中最好使用内置的sn-p,因为有很多东西要填写type:'propdp'并点击两次tab,在这种情况下我们会有:

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(""));

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-28
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-19
      • 2012-09-17
      • 2018-12-10
      相关资源
      最近更新 更多