修改
事实证明,问题在于获取机箱时抛出的错误是将MtObject投射到机箱而未被调试器检测到。
标记Andy的解决方案是正确的,它帮助我看到问题不在ListView代码中,谢谢你和
所以我一直在寻找,似乎无法找到正确的答案 基本上我有一个ListView,我绑定到一个动物数组,一个动物有另一个对象的属性Enclosure,我试图绑定这样的机箱名称
<GridViewColumn Header="Enclosure" DisplayMemberBinding="{Binding Enclosure.Name}" />
这显然是错误的,但我认为显示了我正在努力实现的目标,
基本布局是object.object。
下面的完整ListView
<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);
}
}
答案 0 :(得分: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"
}
});
}
}
}