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

时间:2017-11-10 04:34:38

标签: c# wpf xaml infragistics xamdatagrid

我正在开发一个应用程序,我使用infragistics和wpf来显示数据。 我的代码看起来像这样

<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>

此xaml文件包含四个字段Parameter,value,unit和instances。

 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));
        }
    }
}

当我执行时,它只显示空白屏幕。但我在xaml文件中包含了ItemsSource =“{Binding ViewSetList}”。谁能告诉我为什么绑定没有发生?

我是否还需要绑定XamDataGrid?

帮助将受到高度赞赏。

感谢

1 个答案:

答案 0 :(得分:0)

为什么将XamDataGrid放在ItemsControl内?你不需要ItemsControl

删除它并使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;
    }
}

然后,您可以将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>

您无法绑定到私人字段。