0

编辑
原来的问题是在获取外壳时,它抛出了一个错误,将 MtObject 转换为调试器未检测到的外壳。
将 Andy 的解决方案标记为正确,它帮助我看到问题不在 ListView 代码中,谢谢 andy

所以我一直在四处寻找,似乎无法找到正确的答案。
基本上我有一个ListView,我绑定到一个动物数组,一个动物有另一个对象外壳的属性,我试图像这样绑定外壳名称

<GridViewColumn Header="Enclosure"  DisplayMemberBinding="{Binding Enclosure.Name}" />

这显然是错误的,但我认为显示了我想要实现
的目标,基本布局是 object.object。

下面的完整列表视图

<ListView Margin="20,0,20,0" Grid.Row="1" ItemsSource="{Binding}" x:Name="displayResults" SelectionChanged="displayResults_SelectionChanged_1">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="ListViewItem">
                                <Setter Property="Height" Value="45" />
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.View>
                            <GridView x:Name="gridView">
                                <GridViewColumn Header="Photo" CellTemplate="{StaticResource PhotoTemplate}"/>
                                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding GivenName}" />
                                <GridViewColumn DisplayMemberBinding="{Binding Path=Enclosure.Id}" Header="Enclosure"/>
                                <GridViewColumn Header="Species"  DisplayMemberBinding="{Binding SpeciesName}" />
                                <GridViewColumn Header="DOB"  DisplayMemberBinding="{Binding Dob}" />
                                <GridViewColumn Header="Average Life Span"  DisplayMemberBinding="{Binding AverageLifeSpan}" />
                                <GridViewColumn Header="Natural Habitat"  DisplayMemberBinding="{Binding NaturalHabitat}" />

                            </GridView>
                        </ListView.View>
                    </ListView>

外壳如何获取/设置

public Enclosure Enclosure {
        get {
            return ((Enclosure)(GetSuccessor(GetEnclosureRelationship(Database))));
        }
        set {
            SetSuccessor(GetEnclosureRelationship(Database), value);
        }
    }
4

1 回答 1

1

这对我有用,我想这就是你所追求的?

结果

在此处输入图像描述

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <ListView ItemsSource="{Binding Animals}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Enclosure" DisplayMemberBinding="{Binding Enclosure.Name}"/>
                <GridViewColumn Header="Taste good?" DisplayMemberBinding="{Binding TastesGood}">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Checked="{Binding}"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
</Window>

C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public class Animal : DependencyObject, INotifyPropertyChanged
    {
        Enclosure enclosure;

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Animal), new PropertyMetadata(null));

        public bool TastesGood
        {
            get { return (bool)GetValue(TastesGoodProperty); }
            set { SetValue(TastesGoodProperty, value); }
        }
        public static readonly DependencyProperty TastesGoodProperty = DependencyProperty.Register("TastesGood", typeof(bool), typeof(Animal), new PropertyMetadata(false));

        public Enclosure Enclosure
        {
            get { return enclosure; }
            set
            {
                enclosure = value;
                PropertyChangedEventHandler temp = PropertyChanged;
                if (temp != null)
                {
                    temp(this, new PropertyChangedEventArgs("Enclosure"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class Enclosure : DependencyObject
    {
        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Enclosure), new PropertyMetadata(null));

        public string Location
        {
            get { return (string)GetValue(LocationProperty); }
            set { SetValue(LocationProperty, value); }
        }
        public static readonly DependencyProperty LocationProperty = DependencyProperty.Register("Location", typeof(string), typeof(Enclosure), new PropertyMetadata(null));
    }

    public partial class MainWindow : Window
    {
        public ObservableCollection<Animal> Animals
        {
            get { return (ObservableCollection<Animal>)GetValue(AnimalsProperty); }
            set { SetValue(AnimalsProperty, value); }
        }
        public static readonly DependencyProperty AnimalsProperty = DependencyProperty.Register("Animals", typeof(ObservableCollection<Animal>), typeof(MainWindow), new PropertyMetadata(null));

        public MainWindow()
        {
            InitializeComponent();

            Animals = new ObservableCollection<Animal>();
            Animals.Add(new Animal() { Name = "Cow", TastesGood = true, Enclosure = null });
            Animals.Add(new Animal()
            {
                Name = "Chicken",
                TastesGood = true,
                Enclosure =
                    new Enclosure()
                    {
                        Name = "chicken coop",
                        Location = "outside"
                    }
            });
        }
    }
}
于 2013-03-18T12:39:08.970 回答