简体   繁体   中英

WPF TreeView - Set Color of text based symbol

I'm having issues setting some "prefix" text background and foreground colors in a tree view... The following code sets the correct "symbol" but does not change the colors...

    <DataTemplate DataType="{x:Type models:ChainItem}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Symbol}" FontFamily="Courier New" FontSize="10" Background="{Binding Path=SymbolBackground}" Foreground="{Binding Path=SymbolBrush}"/>
            <TextBlock Grid.Column="1" Text="{Binding Path=Name}" FontSize="16"/>
        </StackPanel>
    </DataTemplate>

Interestingly, if an item with the above data template is selected, the "Symbol" part is black while the rest is white. If I remove the Foreground and Background bindings, then all of the selection is white. If I manually set the colors instead of using bindings, I get the same effect, so I am fairly certain it is not a problem with my binding, but my approach.

I know I can resolve this by using images, but I have been asked to use the text as it can change (the Symbol is actually a 3 character string). I'm considering drawing the text on a bitmap and setting the colors, but I think there is an easier approach.

The answer was that I was using the wrong namespace...

I was using System.Drawing and I needed System.Windows.Media

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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