简体   繁体   English

如何将对象列表绑定到DataGrid?

[英]How to bind a list of objects to a DataGrid?

Background 背景

I'm trying to bind my objects to a DataGrid 我正在尝试将我的对象绑定到DataGrid

I have a list of PhoneVM objects. 我有一个PhoneVM对象列表。 Each item in the list contains a list of StringBindingData . 列表中的每个项目都包含一个StringBindingData列表。 I need each item in StringBindingData to correspond to a different cell in a row of a DataGrid 我需要StringBindingData每个项目对应于DataGrid行中的不同单元格

Each item int he list corresponds to a different value for a user's configuration, including Description and Value . 列表中的每个项目对应于用户配置的不同值,包括DescriptionValue

PhoneVM is defined as: PhoneVM定义为:

public class PhoneVM 
{
    public List<StringBindingData> StringBindingData { get; private set; }

    public PhoneVM()
    {

    }
}

and StringBindingData is defined as: StringBindingData定义为:

public class StringBindingData : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string Description { get; set; }

    private string _value;
    public string Value
    {
        get { return _value; }
        set
        {
            OnPropertyChanged("Value");
        }

    }

    public StringBindingData(string data, string description)
    {
        Value = data;
        Description = description;
    }

    internal void OnPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
}

The Description property of the first item in the StringBindingData list might contain "Name" and the Value property would contain the actual name. StringBindingData列表中第一项的Description属性可能包含“名称”,而Value属性将包含实际名称。 The Description property of the second item in the list might contain "Phone Number" and the Value property would contain the actual number. 列表中第二项的Description属性可能包含“电话号码”,而Value属性将包含实际号码。

Question

I want to get these to bind to a DataGrid . 我想让这些绑定到DataGrid How can I do this? 我怎样才能做到这一点?

What I have so far is an attempt to bind the first column to the Name 到目前为止,我试图将第一列绑定到名称

<DataGrid Name="phoneGrid" AutoGenerateColumns="False" Height="150" Width="Auto" 
            SelectionMode="Single" SelectionUnit="FullRow" Margin="10,10,0,0" 
            HorizontalAlignment="Left" VerticalAlignment="Top" SelectionChanged="phoneGrid_SelectionChanged">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name"
            x:Name="columnPhoneNumber" 
            Binding="{Binding Path=Value}"
            IsReadOnly="True">
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

With this in the code behind: 后面的代码中有这个:

public Phone(List<PhoneVM> phoneVm)
{
    InitializeComponent();

    phoneGrid.DataContext = phoneVm;
}

But even looking at the code it's obviously wrong. 但是即使看代码,这显然也是错误的。 I don't know how to specify for the DataContext that I want it to get the StringBindingData objects from phoneVm and take the data from that. 我不知道如何为DataContext指定我希望它从phoneVm获取StringBindingData对象并从中获取数据。

Is there something I need to add to the XAML to get this working? 我需要添加一些XAML才能使其正常工作吗? Is there something else I can add to the code? 我还可以添加其他代码吗? Do I need to change my data model? 我是否需要更改数据模型?

As you say, you're missing the binding to the StringBindingData property in your view model. 如您所说,您在视图模型中缺少与StringBindingData属性的绑定。 Try adding ItemsSource="{Binding StringBindingData}" to your DataGrid. 尝试将ItemsSource="{Binding StringBindingData}"到您的DataGrid。

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

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