简体   繁体   English

将Datagrid绑定到ObservableCollection <T> 在SIlverlight

[英]Binding Datagrid to ObservableCollection<T> in SIlverlight

I am struggling to bind a DataGrid to an ObservableCollection in SIlverlight. 我正在努力将DataGrid绑定到SIlverlight中的ObservableCollection。

My very simple code is below. 我的非常简单的代码如下。 It currently shows a blank DataGrid. 它目前显示一个空白的DataGrid。 I have gone through tutorials etc and I am sure I am missing something very very basic. 我已经通过了教程等,我确信我遗漏了非常基本的东西。

Main Page XAML 主页XAML

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="Tower.MainPage"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot">
    <sdk:DataGrid Grid.Row="1" Margin="10" IsReadOnly="True" ColumnWidth="120" ItemsSource="{Binding Path=Tests, Mode=OneWay}" AllowDrop="True" />
</Grid>

Main Page Code Behind: 主页代码背后:

public partial class MainPage : UserControl
{
    public ObservableCollection<Test> Tests { get; set; }

    public MainPage()
    {
        InitializeComponent();

        DataContext = this;

        Tests = new ObservableCollection<Test>();
        Tests.Add(new Test() { Label = "Test1" });
        Tests.Add(new Test() { Label = "Test2" });
        Tests.Add(new Test() { Label = "Test3" });
        Tests.Add(new Test() { Label = "Test4" });
    }
}

Test Class: 测试类:

public class Test : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 

    private String _Label;

    public String Label
    {
        get
        {
            return _Label;
        }
        set
        {
            _Label = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Label"));
        }
    } 
}

There are two issues in your code - 您的代码中有两个问题 -

  • You can only bind with property and not with fields. You can only bind with property而不能You can only bind with property字段。 so make Test property first. 所以首先使Test属性。
  • Second, you need to set the DataContext to self for binding to work. 其次,您需要将DataContext to self for设置DataContext to self for绑定工作。

     public partial class MainPage : UserControl { public ObservableCollection<Test> Tests { get; set; } public MainPage() { InitializeComponent(); DataContext = this; Tests = new ObservableCollection<Test>(); Tests.Add(new Test() { Label = "Test1" }); Tests.Add(new Test() { Label = "Test2" }); Tests.Add(new Test() { Label = "Test3" }); Tests.Add(new Test() { Label = "Test4" }); } } 

XAML - XAML -

<Grid x:Name="LayoutRoot">
    <sdk:DataGrid Grid.Row="1" Margin="10" IsReadOnly="True" ColumnWidth="120"
              ItemsSource="{Binding DataContext.Tests,
                RelativeSource={RelativeSource FindAncestor,
                 AncestorType= UserControl}}" AllowDrop="True" />
</Grid>

Notice propertyName - It should be Tests and not tests . 注意propertyName - 它应该是Tests而不是tests This is just a side-note, follow the naming conventions of Microsoft. 这只是一个侧面说明,遵循Microsoft的命名惯例。 Property name's first letter should be uppercase always. 属性名称的第一个字母应始终为大写。

  • For binding to work Tests has to be a public property. 对于绑定工作,测试必须是公共属性。 (I was surprised to see that the property needed to be public but could not get it to work without) (我很惊讶地看到该物业需要公开,但没有它可以让它工作)
  • For referenceing the property in the binding you must either 要参考绑定中的属性,您必须要么
  • Set the datacontext like RV suggested Or reference like this: 设置像RV建议的datacontext或像这样的引用:
<Grid>
    <DataGrid ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=Tests}" />
</Grid>

Don't forget : PUBLIC ObservableCollection tests {get; 不要忘记: PUBLIC ObservableCollection测试{get; private set} 私人套装}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM