【问题标题】:WPF TreeView Binding do not adds elementsWPF TreeView 绑定不添加元素
【发布时间】:2015-09-20 04:35:54
【问题描述】:

我正在尝试使用隐藏在 UI 中的复选框、图像和其他一些信息来创建 TreeView。 XAML 代码:

<TreeView Grid.Column="0" ItemsSource="{Binding T1}">
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Name="CheckBoxZ" Content="{Binding name}" IsChecked="{Binding box}" Foreground="{Binding color}" Unchecked="CheckBoxZ_Updated" Checked="CheckBoxZ_Updated"/>
                        <Image Source="{Binding image}"/>
                    </StackPanel>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

C#代码:

namespace App
{
public partial class MainWindow : Window
{
    public class TS : TreeViewItem
        {
            public string color { get; set; }
            public string name { get; set; }
            public bool box { get; set; }
            public string path { get; set; }
            public string source { get; set; }
            public int operation { get; set; }
            public ImageSource image { get; set; }
            public ObservableCollection<TS> Items { get; set; }
            public TS()
            {
                this.Items = new ObservableCollection<TS>();
            }
        }
    public TS T1, T2;
    public MainWindow()
        {
            InitializeComponent();
            T1 = new TS() { color = "Green", name = "folder", box = true, path = "C:\\folder", source = "D:\\folder", operation = 0, image = X.ToImageSource(System.Drawing.Icon.ExtractAssociatedIcon("D:\\folder\\file.txt")) };
            T2 = new TS() { color = "Green", name = "file.txt", box = true, path = "C:\\folder\\file.txt", source = "D:\\folder\\file.txt", operation = 2, image = X.ToImageSource(System.Drawing.Icon.ExtractAssociatedIcon("D:\\folder\\file.txt")) };
            T1.Items.Add(T2);
            MessageBoxResult result = System.Windows.MessageBox.Show(T1.Items.Count.ToString() + " " + T2.Items.Count.ToString(), "Title", MessageBoxButton.OK, MessageBoxImage.None);
        }
    public static ImageSource ToImageSource(Icon icon)
        {
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            return imageSource;
        }
}

项目已成功添加到变量但不显示。应用程序启动时没有错误。我没有这个错误的变种。怎么了?

【问题讨论】:

    标签: c# wpf binding treeview datatemplate


    【解决方案1】:

    ItemsSource 应该绑定 ObservableCollection 或某些 ItemsCollection(在您的情况下为 Items)。

    我不明白您是如何设计课程的。大致的模式应该如下:

    public class TS: TreeViewItem{}
    public class TSCollection: ObservableCollection<TS> {}
    

    你的MainWindow 应该是这样的:

    public MainWindow()
            {
    TS T1 = new TS(){Name="folder", Color="Green"};
    TS T2= new TS(){Name="folder1", Color="Blue"};
    TSColletion collection = new TSCollection();
    collection.Add(T1,T2);
    }
    

    然后您的 xaml 应该将 collection 设置为 ItemsSource }

    【讨论】:

    • 以及如何添加子项?
    • 我知道您需要 TreeViewItem 中的 ObservableCollection 才能拥有孩子,但我的观点是 ItemsSource 应该始终绑定到项目集合而不是单个项目
    【解决方案2】:

    这里有一些问题:

    1. WPF 只能绑定到 属性 - 它不会绑定到您的 T1 字段
    2. 您正在尝试将TreeView.ItemsSource 绑定到T1。这需要IEnumerable。你应该绑定到类似ObservableCollection&lt;TS&gt;
    3. 您正在使用DataTemplate。由于每个项目本身都可以有子项,因此您应该使用HierarchicalDataTemplate 并将其ItemsSource 绑定到Items 上的Items 属性TS

    顺便说一句,我看不出TS 应该继承TreeViewItem 的理由。

    我建议遵循可以指导您完成此操作的众多教程之一,例如 this one

    【讨论】:

    • 感谢您的回复。 1:我在带有复选框的列表框中使用相同的算法并且它有效。 2.我刚刚尝试这个,问题没有解决。 3.我会试试的。
    • * 抱歉,列表框使用 ObservableCollection
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多