gpt4 book ai didi

c# - 如何从 RGB 颜色创建 WPF 图像?

转载 作者:行者123 更新时间:2023-11-30 21:15:28 28 4
gpt4 key购买 nike

我的 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com