WPF-将嵌套的自定义对象绑定到datagrid

时间:2018-09-18 11:48:01

标签: c# xml wpf

我有一个很大的对象,它的某些属性应显示在数据网格中。问题在于绑定工作不顺利,并且嵌套对象的值为空。

MainCustomObject 是:

public class MainCustomObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public CustomObject1 CustomObject1 { get; set; }
}

CustomObject1 是:

public class CustomObject1
{
    public string Name { get; set; }
    public string Url { get; set; }
}

我的 XAML 代码是:

<DataGrid ItemsSource="{Binding MainCustomObjectList}" AutoGenerateColumns="False">
<DataGrid.Columns>
    <DataGridTextColumn Header="Name" Width="75" Binding="{Binding Name}" />
    <DataGridTextColumn Header="CustomObject1_Name" Width="75" Binding="{Binding CustomObject1.Name}" />
    <DataGridTextColumn Header="CustomObject1_Url" Width="75" Binding="{Binding CustomObject1.Url}" />
</DataGrid.Columns>
</DataGrid>

感谢帮助人员。

2 个答案:

答案 0 :(得分:1)

我测试了您的代码,它以这种方式工作,即创建并显示了一行。您的命名空间或类名称可能不同。

MainWindow

public partial class MainWindow : Window
{
    public IEnumerable<MainCustomObject> MainCustomObjectList { get; set; } = new List<MainCustomObject>
    {
        new MainCustomObject
        {
            Id = 1,
            Name = "Name1",
            CustomObject1 = new CustomObject1
            {
                Name = "Custom name 1",
                Url = "Url 1"
            }
        }
    };
    public MainWindow()
    {
        InitializeComponent();
    }
}

XAML

<Window x:Class="WpfApp2.MainWindow"
    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"
    xmlns:local="clr-namespace:WpfApp2"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
    <DataGrid ItemsSource="{Binding MainCustomObjectList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            <DataGridTextColumn Header="CustomObject1_Name" Binding="{Binding CustomObject1.Name}" />
            <DataGridTextColumn Header="CustomObject1_Url" Binding="{Binding CustomObject1.Url}" />
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>
</Window>

请注意,我将DataContext绑定到了自己的实例。否则,它将显示与您报告的错误相同的错误列表。您还想确保列表完全初始化,如 mm8 所怀疑。如果仅填充第一列,则暗示属性CustomObject1null

答案 1 :(得分:0)

您只能将sed -i属性绑定到sed -i '' '\%...'(包括实现#!/bin/bash option="$1" # this contains -f, -d, or something. shift 1 for filename in "$@"; do ls -al "$filename" # Just an example. But you get the point. done 接口的任何类型)。因此,如果option="$1" shift 1 for filename in "$@"; do if [[ "$option" == '-f' ]]; then exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H,%M,%S.%%e" "$filename" elif [[ "$option" == '-d' ]]; then exiftool -d %Y-%m "-directory<datetimeoriginal" "$filename" elif [[ "$option" == '-fd' ]]; then exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H,%M,%S.%%e" "$filename" exiftool -d %Y-%m "-directory<datetimeoriginal" "$filename" elif [[ "$option" == '-p' ]]; then exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d.%%e" "$filename" else echo -e $arguments fi done 返回一个ItemsSource并且IEnumerable类型具有公共的IEnumerableMainCustomObjectList属性,而这些属性实际上填充了一些值,那么您的XAML标记应该可以正常工作

还要确保IEnumerable<MainCustomObject>中的CustomObject1是定义了Name属性的类的实例。