c# - Treeview MVVM 样式中的 ItemSsource...我无法正确绑定(bind)它

标签 c# silverlight xaml mvvm treeview

我似乎无法为我的 TreeView 使用 ItemSource 标记。没看懂问题。。
在我实际绑定(bind)到我的数据库之前,我正在尝试一个简单的 TreeView 。我正在寻找一个 MVVM 风格的解决方案

这是我的 View 模型

    public class TreeViewVM : ViewModelBase
    { 


        public class Topic
        {
            public string Title { get; set; }
            public int Rating { get; set; }
            private ObservableCollection<Topic> childTopicsValue = new ObservableCollection<Topic>();
            public ObservableCollection<Topic> ChildTopics {
                get
                {
                    return childTopicsValue;
                }
                set
                {
                    childTopicsValue = value;
                }
            }
            public Topic() {}
            public Topic(string title, int rating)
            {
               Title = title;
               Rating = rating;
            }
        }

        static public ObservableCollection<Topic> Users = new ObservableCollection<Topic>();

        public TreeViewVM()
        {


            Users.Add(new Topic("Using Controls and Dialog Boxes", -1));
            Users.Add(new Topic("Getting Started with Controls", 1));
            Topic DataGridTopic = new Topic("DataGrid", 4);
            DataGridTopic.ChildTopics.Add(
                new Topic("Default Keyboard and Mouse Behavior in the DataGrid Control", -1));
            DataGridTopic.ChildTopics.Add(
                new Topic("How to: Add a DataGrid Control to a Page", -1));
            DataGridTopic.ChildTopics.Add(
                new Topic("How to: Display and Configure Row Details in the DataGrid Control", 1));
            Users.Add(DataGridTopic);
            Topics = Users;
        }

        private ObservableCollection<Topic> _Topics { get; set; }
        public ObservableCollection<Topic> Topics
        {
            get
            {
                return _Topics;
            }
            set
            {
                if (_Topics != value)
                {
                    _Topics = value;
                    OnNotifyPropertyChanged("Topics");
                }
            }
        }



    }
}

这是我的 Xaml
xmlns:converter="clr-namespace:TestTree"
xmlns:viewModel="clr-namespace:TestTree.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<UserControl.Resources>
    <viewModel:TreeViewVM x:Key="ViewModel" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource Topics}}">
    <StackPanel x:Name="LayoutRoot2" Background="White">
        <StackPanel.Resources>
            <sdk:HierarchicalDataTemplate x:Key="ChildTemplate"  >
                <TextBlock FontStyle="Italic" Text="{Binding Path=Title}" />
            </sdk:HierarchicalDataTemplate>
            <sdk:HierarchicalDataTemplate x:Key="NameTemplate" 
        ItemsSource="{Binding Path=ChildTopics}" 
        ItemTemplate="{StaticResource ChildTemplate}">
                <TextBlock Text="{Binding Path=Title}" FontWeight="Bold" />
            </sdk:HierarchicalDataTemplate>
        </StackPanel.Resources>
        <sdk:TreeView Width="400"  Height="300" DataContext="{Binding Path=Topics}" ItemTemplate="{StaticResource NameTemplate}" x:Name="myTreeView" />
    </StackPanel>

最佳答案

首先,您将“LayourRoot”网格的 DataContext 设置为具有不存在的关键“主题”的资源。那可能应该是“ViewModel”资源。其次,为什么不能使用 TreeView 上的 ItemsSource 属性?仅在 TreeView 上设置 DataContext 属性将不起作用。这是正确的 XAML:

<UserControl x:Class="MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008">
    <UserControl.Resources>
        <viewModel:TreeViewVM x:Key="ViewModel" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource ViewModel}}">
        <StackPanel x:Name="LayoutRoot2" Background="White">
            <StackPanel.Resources>
                <sdk:HierarchicalDataTemplate x:Key="ChildTemplate"  >
                    <TextBlock FontStyle="Italic" Text="{Binding Path=Title}" />
                </sdk:HierarchicalDataTemplate>
                <sdk:HierarchicalDataTemplate x:Key="NameTemplate" 
        ItemsSource="{Binding Path=ChildTopics}" 
        ItemTemplate="{StaticResource ChildTemplate}">
                    <TextBlock Text="{Binding Path=Title}" FontWeight="Bold" />
                </sdk:HierarchicalDataTemplate>
            </StackPanel.Resources>
            <sdk:TreeView Width="400"  Height="300" ItemsSource="{Binding Path=Topics}" ItemTemplate="{StaticResource NameTemplate}" x:Name="myTreeView" />
        </StackPanel>
    </Grid>
</UserControl>

关于c# - Treeview MVVM 样式中的 ItemSsource...我无法正确绑定(bind)它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4726459/

相关文章:

c# - 获取可在其具体类型中类型转换的相关实体

c# - 两个应用程序之间的声音交互

c# - 将 XML 解析为对象数组 - 使用 Silverlight/Windows Phone

silverlight - 保持模型和ViewModel同步的最佳实践

wpf - 有哪些选项可以在 Silverlight/WPF 中创建带标签的控件列表?

c# - UWP 动画列表项移动

c# - 设置下拉标志之前的下拉事件

c# - 如何在拖动时突出显示目标节点

C# - 如何读取和写入二进制文件?

Silverlight:删除网格中特定单元格的内容