数据绑定:访问AncestorType属性的子进程

时间:2010-04-06 15:04:08

标签: wpf

Bellow是用于审核数据库和wpf的演示应用程序背后的代码和Xaml。 问题是将Store.ImagePath属性绑定到person节点不起作用。那是图像没有显示。

<Image Source="{Binding Path=Store.ImagePath, RelativeSource={RelativeSource AncestorType={x:Type local:Store}}}" />

这是代码隐藏

namespace TreeViewDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Customers customers = new Customers();
        customers.Users = new List<Person> 
        { 
            new Person { Name = "John"},
            new Person { Name = "Adam"}, 
            new Person { Name = "Smith"}
        };

        Store store = new Store();
        store.AllCustomers.Add(customers);
        this.DataContext = store;
    }
}

public class Store : INotifyPropertyChanged
{
    string imagePath = "imageone.png";

    public Store()
    {
        AllCustomers = new ObservableCollection<Customers>();
    }

    public string StoreName
    {
        get
        {
            return "ABC Store";
        }
    }
    public ObservableCollection<Customers> AllCustomers
    {
        get;
        set;
    }
    public string ImagePath
    {
        get
        {
            return imagePath;
        }
        set
        {
            if (value == imagePath) return;
            imagePath = value;

            this.OnPropertyChanged("ImagePath");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
} 
public class Customers
{
    public string Label
    {
        get
        {
            return string.Format("People({0})", Users.Count());
        }
    }
    public List<Person> Users
    {
        get;
        set;
    } 
}
public class Person : INotifyPropertyChanged
{
    public string Name
    {
        get;
        set;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
}

这是Xaml。

<Window x:Class="TreeViewDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TreeViewDemo"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources >
   <DataTemplate DataType="{x:Type local:Person}" x:Key="personKey" >
        <StackPanel Orientation="Horizontal" >
            <Image Source="{Binding Path=Store.ImagePath, RelativeSource={RelativeSource AncestorType={x:Type local:Store}}}" />
            <TextBlock Text="{Binding Name}" />
        </StackPanel>
    </DataTemplate>
    <HierarchicalDataTemplate x:Key="customerKey" ItemsSource="{Binding Users}" ItemTemplate="{StaticResource personKey }" >
        <TextBlock Text="{Binding Label}" FontWeight="Bold"/>
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
    <Canvas>
        <Button HorizontalAlignment="Left" DockPanel.Dock="Top" Height="29" Width="112" Canvas.Left="123" Canvas.Top="5">Image one</Button>  <Button HorizontalAlignment="Left" VerticalAlignment="Top" DockPanel.Dock="Top" Height="28" Width="119" Canvas.Left="249" Canvas.Top="7">Image two</Button>
        <TreeView  HorizontalAlignment="Stretch"  Name="treeView1" VerticalAlignment="Stretch" 
               ItemsSource="{Binding .}"  Height="260" Width="363" Canvas.Left="81" Canvas.Top="45">
            <TreeViewItem ItemsSource="{Binding AllCustomers}" ItemTemplate="{StaticResource customerKey}" Header="{Binding StoreName}"></TreeViewItem>
        </TreeView>
    </Canvas>
</Grid>
</Window>

所有文件都在同一目录中。

由于

1 个答案:

答案 0 :(得分:3)

相对来源用于在可视树中查找对象。您要求它在可视树中找到最近的Store。由于商店甚至不能在可视树中,因此查找将失败并产生null。您真正想要的是根DataContext的{​​{1}},因为这是您Window的所在地:

Store