【发布时间】: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