简体   繁体   English

WPF-无法将XamDatagrid与可观察的集合绑定

[英]WPF- Not able to Bind XamDatagrid with observable collection

I am doing developing one application where i am using infragistics and wpf to display data. 我正在开发一个应用程序,其中使用基础设施和wpf显示数据。 My code looks like this 我的代码看起来像这样

<Grid>
    <ItemsControl x:Name="MyItemContol"  ItemsSource="{Binding ViewSetList}" 
VerticalAlignment="Stretch" HorizontalAlignment="Stretch">

        <ItemsControl.ItemTemplate>
            <DataTemplate>

                <igDP:XamDataGrid  DataSource="{Binding ViewSetList}" >
                    <igDP:XamDataGrid.FieldLayouts>
                        <igDP:FieldLayout>
                            <igDP:FieldLayout.Fields>

                                <igDP:Field Name="PARAMETER">
                                    <igDP:Field.Settings>
                                        <igDP:FieldSettings AllowEdit="False" />
                                    </igDP:Field.Settings>

                                </igDP:Field>

                                <igDP:Field Name="VALUE">
                                    <igDP:Field.Settings>
                                        <igDP:FieldSettings AllowEdit="False"   />
                                    </igDP:Field.Settings>
                                </igDP:Field>
                                <igDP:Field Name="UNIT">
                                    <igDP:Field.Settings>
                                        <igDP:FieldSettings AllowEdit="False" />
                                    </igDP:Field.Settings>
                                </igDP:Field>
                                <igDP:Field Name="INSTANCE">
                                    <igDP:Field.Settings>
                                        <igDP:FieldSettings AllowEdit="False" />
                                    </igDP:Field.Settings>
                                </igDP:Field>

                            </igDP:FieldLayout.Fields>
                        </igDP:FieldLayout>
                    </igDP:XamDataGrid.FieldLayouts>

                </igDP:XamDataGrid>


            </DataTemplate>
        </ItemsControl.ItemTemplate>

    </ItemsControl>
</Grid>

This xaml file contains four fields Parameter,value,unit and instances. 该xaml文件包含四个字段:参数,值,单位和实例。

 public partial class MainWindow : Window 
    {
        private ObservableCollection<Parameters> viewSetList = new 
        ObservableCollection<Parameters>();

        public MainWindow()
        {
        InitializeComponent();

        //Add data to the collection
        viewSetList.Add(new Parameters() { PARAMETER="abc",  INSTANCE="def" , UNIT="hhshhd", VALUE="hahha" });


    }


public class Parameters:  INotifyPropertyChanged
{
    private string parameterName = string.Empty;

    public string PARAMETER
    {
        get { return parameterName; }
        set { parameterName = value; }
    }
    private string parameterValue = string.Empty;

    public string VALUE
    {
        get { return parameterValue; }
        set { parameterValue = value; }
    }
    private string parameterUnit = string.Empty;

    public string UNIT
    {
        get { return parameterUnit; }
        set { parameterUnit = value; }
    }
    private string instance = string.Empty;

    public string INSTANCE
    {
        get { return instance; }
        set { instance = value; }
    }       

    public event PropertyChangedEventHandler PropertyChanged;
    private void onPropertyChanged(object sender, string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
        }
    }
}

When i executed it is displaying only blank screen. 当我执行它只显示空白屏幕。 But i have included ItemsSource="{Binding ViewSetList}" in the xaml file. 但是我在xaml文件中包含了ItemsSource =“ {Binding ViewSetList}”。 Could any one tell me why the binding is not happening? 谁能告诉我为什么绑定没有发生?

Should i need bind XamDataGrid also? 我还需要绑定XamDataGrid吗?

Help will be highly appreciated. 非常感谢您的帮助。

THANKS 谢谢

Why did you put the XamDataGrid inside an ItemsControl ? 为什么将XamDataGrid放在ItemsControl You don't need the ItemsControl . 您不需要ItemsControl

Remove it and make ViewSetList a public property and set the DataContext of the window to itself: 删除它,并将ViewSetList设置为公共属性 ,并将窗口的DataContext设置为其自身:

public partial class MainWindow : Window
{
    private ObservableCollection<Parameters> _viewSetList = new ObservableCollection<Parameters>();
    public ObservableCollection<Parameters> ViewSetList { get { return _viewSetList; } }

    public MainWindow()
    {
        InitializeComponent();

        //Add data to the collection
        _viewSetList.Add(new Parameters() { PARAMETER = "abc", INSTANCE = "def", UNIT = "hhshhd", VALUE = "hahha" });

        DataContext = this;
    }
}

You can then bind the XamDataGrid to the property: 然后,您可以将XamDataGrid绑定到属性:

<Grid>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <igDP:XamDataGrid DataSource="{Binding ViewSetList}" >
                <igDP:XamDataGrid.FieldLayouts>
                    <igDP:FieldLayout>
                        <igDP:FieldLayout.Fields>
                            <igDP:Field Name="PARAMETER">
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings AllowEdit="False" />
                                </igDP:Field.Settings>
                            </igDP:Field>
                            <igDP:Field Name="VALUE">
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings AllowEdit="False"   />
                                </igDP:Field.Settings>
                            </igDP:Field>
                            <igDP:Field Name="UNIT">
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings AllowEdit="False" />
                                </igDP:Field.Settings>
                            </igDP:Field>
                            <igDP:Field Name="INSTANCE">
                                <igDP:Field.Settings>
                                    <igDP:FieldSettings AllowEdit="False" />
                                </igDP:Field.Settings>
                            </igDP:Field>

                        </igDP:FieldLayout.Fields>
                    </igDP:FieldLayout>
                </igDP:XamDataGrid.FieldLayouts>
            </igDP:XamDataGrid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</Grid>

You cannot bind to private fields. 您不能绑定到私有字段。

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

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