2

I have some colors for my Layer objects that are shown in a TreeView. Right now I use something like this:

<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>

The original color values are gonna be accessed (binded) from the Layer itself, so like:

layer.Color

of type System.Drawing.Color. But I can change the type to be something else if it would make things easier.

What's the best way to do this in terms of performance and elegance?

I will have a couple thousand TreeView items it that makes a difference.

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

1 Answers1

4

I did not try it myself with a TreeView (only with a ListBox), but maybe this is worth a try:

<DataTemplate>
    <!-- your additional container -->
        <Canvas Width="15" Height="15">
             <Canvas.Background>
                 <SolidColorBrush Color="{Binding Path=ColorProperty}" />
             </Canvas.Background>
        </Canvas>
    <!-- end of container -->
</DataTemplate>

This approach uses a direct binding to the color property, so no images need to be created (e.g. by a Converter).

But it is definitely recommended to try it out to see if it works with thousands of items.

jCoder
  • 2,289
  • 23
  • 22
  • Thanks but that ColorProperty, what type should it be? System.Drawing.Color? Also you used Canvas, that would be better than Image? Just wondering. – Joan Venge Apr 21 '11 at 21:56
  • 1
    Yes, ColorProperty would be of type `System.Drawing.Color` and I suppose that `Canvas` (or any other simple visual control, even a Grid works) needs less resources than an image, because you would need to create a matching image in memory and even with caching this would take more resources than a simple control. – jCoder Apr 21 '11 at 22:01
  • So Canvas would be lighter even than a Grid? I will use Canvas, just curious. Lastly do you know what event I can use to do something when the Canvas is clicked? I found MouseDown, so I guess that should work? – Joan Venge Apr 21 '11 at 22:09
  • 1
    As `Canvas` and `Grid` are both derived from `Panel` there should be no great difference (at least they do not need any additional resources like an `Image` will need), but this is just a _good_ _guess_. There should be `MouseLeftButtonDown` but if you see `MouseDown` then that should work too. – jCoder Apr 21 '11 at 22:22
  • Thanks that makes sense. Btw what's the difference between `MouseLeftButtonDown` and `MouseDown`? I assume the latter provides all access whereas the first one only fires for left button down. But still one would fire before the other, right? Which one that would be? – Joan Venge Apr 21 '11 at 22:25
  • 1
    Yes, your assumption is right, and doing a little test it seems that `MouseLeftButtonDown` is likely to be fired before `MouseDown` which makes sense, because the latter one is more "generic". But I would not rely on this until verification in MSDN. – jCoder Apr 21 '11 at 22:33