【问题标题】:ListView Binding array of objects access variablesListView 绑定对象数组访问变量
【发布时间】:2013-03-06 18:56:04
【问题描述】:

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

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

<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);
        }
    }

【问题讨论】:

    标签: c# wpf listview object binding


    【解决方案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"
                        }
                });
            }
        }
    }
    

    【讨论】:

    • 这似乎在这种情况下工作,但是我的 Animal 类继承了 MtObject 因为我连接到 Matisse 对象数据库,是否可以在不需要 DependencyObject 的情况下做到这一点?
    • 如果初始化后需要更新属性,则需要实现INotifyPropertyChanged,才会更新
    • 在更新中,我删除了 Enclosure 的依赖属性,而是让它调用 INotifyPropertyChanged.PropertyChanged 事件。
    • 现在让我说我不需要更新属性,我可以简单地显示数据而不需要 DependencyObject 的最简单方法是什么,所以如果我们只有简单的类定义有没有办法访问绑定数据
    • 我认为使用 {Binding Enclosure.Name} 的唯一要求是它是一个属性。
    猜你喜欢
    • 2014-01-05
    • 2018-12-31
    • 2011-05-20
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2019-09-07
    相关资源
    最近更新 更多