C# 图像绑定wpf
我试图生成一个元素,让我能够使用WPF将一些图像显示到C# 图像绑定wpf,c#,.net,wpf,C#,.net,Wpf,我试图生成一个元素,让我能够使用WPF将一些图像显示到treeview。我认为我必须生成自己的TreeViewItem,以便将我想要的属性绑定到TreeView控件中。这是我自己的TreeViewItem: public class TreeViewItem : System.Windows.Controls.TreeViewItem { public ImageSource Image { get; set; } public TreeViewItem(string text
treeview
。我认为我必须生成自己的TreeViewItem
,以便将我想要的属性绑定到TreeView
控件中。这是我自己的TreeViewItem
:
public class TreeViewItem : System.Windows.Controls.TreeViewItem
{
public ImageSource Image { get; set; }
public TreeViewItem(string text, ImageSource displayedImage)
{
this.Header = text;
this.Image = displayedImage;
}
}
生成此对象后,我定义自定义树视图的结构,以便绑定所有数据:
<UserControl x:Class="test.TreeControl"
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"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="100">
<Grid>
<TreeView Name="TVTree" x:FieldModifier="public">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<Grid Margin="2" MinHeight="25" MaxHeight="25" MinWidth="60">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="{Binding Image}" Height="16" Width="16"/>
<TextBlock Grid.Column="1" Text="{Binding Header}" Margin="5,0"/>
</Grid>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
据我所知,我正在尝试将图像源与自定义控件的图像源绑定,并将文本块的文本与我的treeviewItem
的标题绑定。问题是控件没有显示图像,我发现标题也没有显示。它只显示ToString()
方法的结果(与定义为对象标题的字符串相同)
有人知道如何正确绑定此数据吗?
提前感谢您的绑定正在
DataContext
中查找图像
和标题
,这将是标准MVVM WPF中的VM。如果在VisualStudio中检查输出窗口,您将看到绑定错误
您不需要TreeViewItem
类,您已经用模板定义了树项目的视图
您确实需要绑定到
TreeItemVM
。将您的属性添加到其中。它们应该只是数据。无可视类。使用数据模板时,需要将数据定义为类型,例如TreeItem
及其在模板中的外观,此处为树视图的层次数据模板
。TreeItem
可能是这样的
public class TreeItem
{
public string Header { get; }
public ImageSource Image { get; }
public ObservableCollection<TreeItem> Children { get; }
public TreeItem(string header, ImageSource displayedImage, ObservableCollection<TreeItem> children)
{
Header = header;
Image = displayedImage;
Children = children;
}
}
接下来,您必须创建TreeControlViewModel
的实例,并将其分配给TreeControl
用户控件,以便可以绑定到其TreeItems
属性,该属性是TreeView
的集合
<UserControl x:Class="test.TreeControl"
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"
mc:Ignorable="d"
d:DesignHeight="200"
d:DesignWidth="100">
<UserControl.DataContext>
<local:TreeControlViewModel/>
</UserControl.DataContext>
<!-- ...your tree view XAML code. -->
</UserControl
请注意,observateCollection
将在您使用例如Add
或Remove
修改项目集合时通知并触发更新TreeView
。如果您对修改诸如标题
之类的属性也感兴趣,您应该查看INotifyPropertyChanged
an,以便在用户界面中也为它们启用更新。为了简单起见,并关注您的问题,我将跳过这一部分
<UserControl x:Class="test.TreeControl"
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"
mc:Ignorable="d"
d:DesignHeight="200"
d:DesignWidth="100">
<UserControl.DataContext>
<local:TreeControlViewModel/>
</UserControl.DataContext>
<!-- ...your tree view XAML code. -->
</UserControl
<TreeView Name="TVTree" ItemsSource="{Binding TreeItems}">