C#WPF数据网格绑定IEnumerable<;someclass>;

C#WPF数据网格绑定IEnumerable<;someclass>;,c#,wpf,datagrid,C#,Wpf,Datagrid,我想在WPF MVVM中执行一些DataGrid。我是新来的 为什么我的简单解决方案不起作用 我想把这个绑起来: public IEnumerable<ZenonClasses.RGMRecipeValue> Values { get => SelectedPhase?.ValueItems; } 公共IEnumerable值 { get=>SelectedPhase?.ValueItems; } 为此:

我想在WPF MVVM中执行一些DataGrid。我是新来的

为什么我的简单解决方案不起作用

我想把这个绑起来:

public IEnumerable<ZenonClasses.RGMRecipeValue> Values
        {
            get => SelectedPhase?.ValueItems;
        }
公共IEnumerable值
{
get=>SelectedPhase?.ValueItems;
}
为此:

        <DataGrid ItemsSource="{Binding Values.VarName}" HorizontalAlignment="Left" Height="171" Margin="10,0,0,0" Grid.Row="4" Grid.RowSpan="5" VerticalAlignment="Top" Width="380" Grid.ColumnSpan="2"/>

但是我的数据网格是空的。
我想我需要一些配置,我正在寻找简单的解决方案,不幸的是,我没有找到足够好的解释。

首先,我假设您的窗口(xaml)有一个datacontext,它是实现您提到的属性(值)的模型视图类。 如果没有,那么你必须确保这种情况发生。我通常做的是在窗口代码后面指定它:

    public MainWindow()
    {
        InitializeComponent();
        vm = new YOURVIEWMODELCLASS();
        DataContext = vm;
    }
然后,您的DataGrid控件需要有列的定义,我在您的代码示例中没有看到这一点,但您需要提供它们,以便控件知道要“绘制”多少列,下面详细说明:

然后是“值”属性本身。您当前的类型是IEnumerable,您可能需要将此类型更改为ObservableCollection,因为它更适合WPF中的数据绑定

最后,确保ModelView类实现InotifyPropertyChanged接口,因为它是WPF提供的使数据绑定有效工作的机制。以下是我的一个应用程序的示例代码:

public class MainWindowViewModel : INotifyPropertyChanged
{
 private ObservableCollection<Results> searchResults;

 public ObservableCollection<Results> SearchResults { get => searchResults; set { searchResults = value; NotifyPropertyChanged(); } }


#region INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

#endregion

//Other code here.... 

}
public类MainWindowViewModel:INotifyPropertyChanged
{
私人可观察收集搜索结果;
public observeCollection SearchResults{get=>SearchResults;set{SearchResults=value;NotifyPropertyChanged();}}
#区域inotifyproperty已更改
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
#端区
//其他代码在这里。。。。
}
在这种情况下,my Property SearchResults将使用如下数据网格显示在窗口中:

<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="10" ItemsSource="{Binding SearchResults}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Term" Binding="{Binding SearchedTopic}"/>
            <DataGridTextColumn Header="Match Count" Binding="{Binding FoundPaths.Count}" />
        </DataGrid.Columns>
        <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <DataGrid Margin="5" ItemsSource="{Binding FoundPaths}" AutoGenerateColumns="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Found" Binding="{Binding}"/>
                    </DataGrid.Columns>
                </DataGrid>
            </DataTemplate>
        </DataGrid.RowDetailsTemplate>
    </DataGrid>


希望这有帮助。

您需要将属性设置为True。试试这个
绑定observablecollection而不是ienumerable是很常见的。您需要该Values属性位于datagrid的datacontext中(它将从它所在的窗口继承),并将itemssource绑定到该属性。您不需要将autogeneratecolumns设置为true-这是默认值。是否设置了?您应该在Values属性上绑定
ItemSource
,而不是
Values.VarName
,以引用文档:“将IEnumerable绑定到ItemsControl会强制WPF创建一个包装IList对象,这意味着您的性能会受到第二个对象不必要的开销的影响。”如果将网格绑定到ObservableCollection,则不需要在该ObservableCollection上使用INotifyPropertyChanged,因为它已经实现了。