我的Layer
对象有一些颜色,这些颜色显示在TreeView
中。现在我使用这样的东西:
<GridViewColumn Width="300">
<GridViewColumnHeader Content="Layers" />
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel MouseLeftButtonDown="Layers_MouseLeftButtonDown" Orientation="Horizontal">
<Image Width="15"
Height="15"
Source="{Binding ImageFromColor}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
原始颜色值将从Layer
本身访问(绑定(bind)),如下所示:
layer.Color
类型为System.Drawing.Color
。但如果可以让事情变得更容易,我可以将类型更改为其他类型。
在性能和优雅方面做到这一点的最佳方法是什么?
我将拥有几千个 TreeView
项目,它们会有所作为。
最佳答案
我自己没有尝试使用 TreeView(仅使用 ListBox),但也许这值得一试:
<DataTemplate>
<!-- your additional container -->
<Canvas Width="15" Height="15">
<Canvas.Background>
<SolidColorBrush Color="{Binding Path=ColorProperty}" />
</Canvas.Background>
</Canvas>
<!-- end of container -->
</DataTemplate>
此方法使用直接绑定(bind)到颜色属性,因此不需要创建图像(例如通过转换器)。
但绝对建议尝试一下,看看它是否适用于数千个项目。
关于c# - 如何从 RGB 颜色创建 WPF 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5749974/