Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 图像绑定wpf_C#_.net_Wpf - Fatal编程技术网

C# 图像绑定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

我试图生成一个元素,让我能够使用WPF将一些图像显示到
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}">