绑定到ObservableCollection <t> </t>中的类

时间:2011-08-01 13:06:44

标签: silverlight data-binding observablecollection

我正在尝试将Silverlight Datagrid绑定到ObservableCollection<T>

具体来说,我有ObservableCollection<Target>,其中target具有以下属性:Hierarchy和Item。

我只希望datagrid显示Hierarchy类的信息,但是当用户选择一行时,我希望Target类的Hierarchy和item属性在代码中可用。

这可能吗?

感谢。

2 个答案:

答案 0 :(得分:1)

在列中指定层次结构属性的路径,如下所示:

Binding="{Binding Hierarchy.Title}"

以下完整示例:

MainPage.xaml中

<UserControl x:Class="GridBinding.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid ItemsSource="{Binding Targets}" AutoGenerateColumns="False" Name="dataGrid1" SelectionChanged="dataGrid1_SelectionChanged">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Header="Title" Binding="{Binding Hierarchy.Title}" Width="Auto" />
                <sdk:DataGridTextColumn Header="Text" Binding="{Binding Hierarchy.Text}" Width="Auto" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>
</UserControl>

MainPage.cs

using System.Collections.ObjectModel;
using System.Windows.Controls;

namespace GridBinding
{
    public partial class MainPage : UserControl
    {
        public ObservableCollection<Target> Targets { get; set; }

        public MainPage()
        {
            InitializeComponent();
            this.Targets = new ObservableCollection<Target>()
                            {
                                new Target(){ Hierarchy = new Hierarchy(){Text = "Text 1", Title = "Title 1"}, Item = new Item(){Name = "Name 1"}},
                                new Target(){ Hierarchy = new Hierarchy(){Text = "Text 2", Title = "Title 2"}, Item = new Item(){Name = "Name 2"}},
                                new Target(){ Hierarchy = new Hierarchy(){Text = "Text 3", Title = "Title 3"}, Item = new Item(){Name = "Name 2"}},
                            };
            this.DataContext = this;
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            Target target = dataGrid.SelectedItem as Target;
            Hierarchy heirarchy = target.Hierarchy;
            Item item = target.Item;
        }
    }

    public class Target
    {
        public Hierarchy Hierarchy { get; set; }
        public Item Item { get; set; }
    }

    public class Hierarchy
    {
        public string Title { get; set; }
        public string Text { get; set; }
    }

    public class Item
    {
        public string Name { get; set; }
    }
}

答案 1 :(得分:0)

是的,这是可能的。首先,在网格上将AutoGenerateColumns设置为false,然后手动为要显示的属性添加列。将ObservableCollection<Target>设置为ItemsSource的{​​{1}}。

当用户选择一行时,所选的DataGrid实例将通过Target属性提供,允许您查看其所有属性。