简体   繁体   English

如何以编程方式替换TreeViewItem的图标?

[英]How to programmatically replace the icon of a TreeViewItem?

In my current project I've got a WPF TreeView and added items like this: 在我当前的项目中,我有一个WPF TreeView并添加了以下内容:

<TreeViewItem Name="treeViewItem6" IsEnabled="False">
  <TreeViewItem.Header>
    <StackPanel Orientation="Horizontal">
        <Image Height="16" Source="Images/16x16_green_lamp.png" Width="16" />
        <TextBlock Margin="5,0" Text="Status: Connected" />
    </StackPanel>
  </TreeViewItem.Header>
</TreeViewItem>

I would like to replace an icon as well as the text of a TextBlock in my C# code to make it look like this: 我想在我的C#代码中替换图标以及TextBlock的文本,以使其看起来像这样:

在此处输入图片说明

Is there an easy way to programmatically replace the icon as well as the text of a TreeViewItem or do I have to iterate through the entire tree of sub items? 有没有一种简便的方法可以以编程方式替换TreeViewItem的图标以及文本,还是我必须遍历整个子项树? (Unfortunately I'm a WPF novice and I'm more used to the good old WinForms) (不幸的是,我是WPF的新手,我更习惯于老式的WinForms)

To change the source of the image and the text of a textblock you can give the controls a name like this: 要更改图像的来源和文本块的文本,可以为控件命名,如下所示:

<Image Name="imgIcon" Height="16" Source="Images/16x16_green_lamp.png" Width="16" />
<TextBlock Name="tbStatus" Margin="5,0" Text="Status: Connected" />

That way you can change the properties you want to change easily programmatically, like this: 这样,您可以通过编程轻松更改要更改的属性,如下所示:

Image img = FindResource("red") as Image;
if (img != null) imgIcon.Source = img.Source;

tbStatus.Text = "Status: Disconnected";

To find the "green" and "red" resources you can add them to the XAML like this: 要查找“绿色”和“红色”资源,可以将它们添加到XAML中,如下所示:

<Window.Resources>
    <Image x:Key="green" Source="Images/16x16_green_lamp.png" />
    <Image x:Key="red" Source="Images/16x16_red_lamp.png" />
</Window.Resources>

Of course you will need additional logic to determine the status, but this will get you started. 当然,您将需要其他逻辑来确定状态,但这将使您入门。

Instead of hardcoding the path to your image you can actually bind the image source to a string property in your ViewModel. 实际上,您可以将图像源绑定到ViewModel中的字符串属性,而不是对图像的路径进行硬编码。 The string property needs to represent the uri to the image you want to display. string属性需要代表您要显示的图像的uri。 Once you change the string property (and fire the OnPropertyChanged event) your UI will automatically change the image. 一旦更改了字符串属性(并触发OnPropertyChanged事件),您的UI就会自动更改图像。 WPF has a build in converter for images so you should not worry too much about that. WPF具有用于图像的内置转换器,因此您不必为此担心太多。 Here is how your binding should look like: 绑定的外观如下所示:

<Image Source="{Binding ImageSource}" />

Where ItemSource is a string property in your view model. 其中ItemSource是视图模型中的字符串属性。

Hope that helps. 希望能有所帮助。

How do you generate the tree? 您如何生成树? Is there any Data Binding there? 那里有数据绑定吗? If the tree is constructed manually, why don't you simply give names to the elements that you want to change, and then in code reference those elements: 如果树是手动构建的,为什么不简单地给要更改的元素命名,然后在代码中引用这些元素:

<TreeViewItem Name="treeViewItem6" IsEnabled="False">
    <TreeViewItem.Header>
        <StackPanel Orientation="Horizontal">
            <Image x:Name="StatusImage" Height="16" Source="Images/16x16_green_lamp.png" Width="16" />
            <TextBlock x:Name="StatusText" Margin="5,0" Text="Status: Connected" />
        </StackPanel>
    </TreeViewItem.Header>
</TreeViewItem>

Then, in C# code: 然后,在C#代码中:

...
this.StatusImage.Source="...";  // a new ima
this.StatusText.Text = "....;

For the image source, you'll need to generate it (text is not enough): new BitmapSource( new Uri( "image uri" ) ) 对于图像源,您需要生成它(文本是不够的):new BitmapSource(new Uri(“ image uri”))

You should construct it once and cache it. 您应该构造一次并缓存它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM