【问题标题】:Silverlight Treeview inline HierarchicalDataTemplate binding issueSilverlight Treeview 内联 HierarchicalDataTemplate 绑定问题
【发布时间】:2012-03-13 17:08:40
【问题描述】:

我有 MyPOCO 对象的 MyCollection(具有两个字符串属性)。

当我尝试在树视图中实现 HierarchicalDataTemplate 时,绑定不起作用,我得到了类名。

我知道如果我从控件中取出数据模板一切正常,但我很想知道为什么这个示例不起作用。

<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource testVM}}">
    <sdk:TreeView Margin="8,8,8,111" ItemsSource="{Binding MyCollection}">
        <sdk:HierarchicalDataTemplate ItemsSource="{Binding MyPOCO}">
            <sdk:HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Property1}"/>
                        <TextBlock Text="{Binding Property2}"/>
                    </StackPanel>
                </DataTemplate>
            </sdk:HierarchicalDataTemplate.ItemTemplate>
        </sdk:HierarchicalDataTemplate>
    </sdk:TreeView>
</Grid>

这也是 ViewModel。

namespace MyPOCProject

{ 公共课 MyPOCO { 私有字符串属性1;

    public string Property1
    {
        get { return property1; }
        set { property1 = value; }
    }

    private string property2;

    public string Property2
    {
        get { return property2; }
        set { property2 = value; }
    }

    public MyPOCO(string p1, string p2)
    {
        property1 = p1;
        property2 = p2;
    }
}

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<MyPOCO> myCollection;

    public ObservableCollection<MyPOCO> MyCollection
    {
        get { return myCollection; }
        set { myCollection = value; RaisePropertyChanged("MyCollection"); }
    }

    public MyViewModel()
    {
        MyPOCO _poco1 = new MyPOCO("aaa1", "bbb1");
        MyPOCO _poco2 = new MyPOCO("aaa2", "bbb2");
        MyPOCO _poco3 = new MyPOCO("aaa3", "bbb3");

        MyCollection = new ObservableCollection<MyPOCO>() { _poco1, _poco2, _poco3 };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

}

那我做错了什么?

再次...我对这个特定的例子很感兴趣。我想知道这个例子有什么问题以及为什么。

谢谢。

【问题讨论】:

  • 您还有更多代码吗?我想看看课程。
  • 我更新了帖子。谢谢 Silvermind。

标签: silverlight xaml


【解决方案1】:

您发布的代码不是分层的,换句话说:MyPOCO 类不包含属性MyCollection&lt;MYPOCO&gt; Children。 这是 HierarchicalDataTemplate 的示例

Xaml:

<sdk:TreeView x:Name="MyTreeView"
              HorizontalAlignment="Left"
              ItemsSource="{Binding MyCollection}"
              Width="200" Height="280">
    <sdk:TreeView.ItemTemplate>
        <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
            <TextBlock Text="{Binding Path=Header}"/>
            <sdk:HierarchicalDataTemplate.ItemTemplate>
                <sdk:HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                    <TextBlock Text="{Binding Path=Header}"/>
                </sdk:HierarchicalDataTemplate>
            </sdk:HierarchicalDataTemplate.ItemTemplate>
        </sdk:HierarchicalDataTemplate>
    </sdk:TreeView.ItemTemplate>
</sdk:TreeView>

代码隐藏类:

public class MyPoco
{
    public MyPoco(string header, int sampleChildrenCount)
    {
        this.Header = header;
        this.Children = new ObservableCollection<MyPoco>();
        if (sampleChildrenCount > 0)
        {
            for (int i = 0; i < sampleChildrenCount; i++)
            {
                string newHeader = String.Format("Test {0}", sampleChildrenCount * i);
                var myPoco = new MyPoco(newHeader, sampleChildrenCount - 1)
                this.Children.Add(myPoco);
            }
        }
    }
    public string Header { get; set; }
    public ObservableCollection<MyPoco> Children { get; set; }
}

public class MyViewModel
{
    public MyViewModel()
    {
        MyCollection = new ObservableCollection<MyPoco>();
        for (int i = 0; i < 6; i++)
        {
            this.MyCollection.Add(new MyPoco(String.Format("Test {0}", i), 5));
        }
    }
    public ObservableCollection<MyPoco> MyCollection { get; set; }
}

代码隐藏启动:

public MainPage()
{
    public MainPage()
    {
        InitializeComponent();
        MyTreeView.DataContext = new MyViewModel();
    }
}

【讨论】:

  • 当时的想法是,基于从 WebService 返回的任何内容,我应该能够在 TreeView 中显示它。想象一下,当您的根目录中只有几个目录时。因为当我使用模板时相同的代码可以工作,所以我真的怀疑我必须像你提到的那样创建对象结构。我认为问题来自控件以及它附加模板资源与控件中指定的内联模板的方式。感谢您的回复。
  • 但是如果代码不是分层的,它就不能在这样的 HierarchicalDataTemplate 中工作。即使您没有填充任何孩子,也请尝试使其分层。否则,您应该使用适用于非分层数据的 DataTemplate。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多