Color of rendered image of button from Path not changed when theme changes

Ribbon for WPF Forum

Posted 5 months ago by Procam
Version: 18.1.0675
Avatar

In our application the ribbon buttons are created dynamically in code, the images for the button (property ImageSourceSmall) are converted from Paths (defined in similar Resources as below) to Imagesource.

    <Style x:Key="CrossIcon" TargetType="{x:Type ContentControl}">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                        <Grid>
                            <Grid Name="backgroundGrid" Width="48" Height="48" Visibility="Collapsed" />
                            <Path Data="M7.1999998,0L16,8.7999997 24.799999,0 32,7.1999998 23.2,16 32,24.799999 24.799999,32 16,23.2 7.1999998,32 0,24.799999 8.7999997,16 0,7.1999998z" Stretch="Uniform" 
                              Fill="{DynamicResource {x:Static themes:AssetResourceKeys.ControlForegroundNormalBrushKey}}" Width="26" Height="26" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5">
                                <Path.RenderTransform>
                                    <TransformGroup>
                                        <TransformGroup.Children>
                                            <RotateTransform Angle="0" />
                                            <ScaleTransform ScaleX="1" ScaleY="1" />
                                        </TransformGroup.Children>
                                    </TransformGroup>
                                </Path.RenderTransform>
                            </Path>
                        </Grid>
                    </Viewbox>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

We do not use png images for the button images.

Everything work ok but when I change the theme to another e.g.:

ThemeManager.CurrentTheme = ThemeName.MetroDark.ToString();

so the color of image for the button is not changed, it stays according to the initial defined theme. I suspect it is for the way I create the images in converter. How to achive the color of these images to be changed properly? Where I can send a sample app?

Thanks!

[Modified 5 months ago]

Comments (1)

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Thanks for the sample.  I believe the problem here is that your XamlToImageConverter.Convert method ends up building a RenderTargetBitmap from the XAML and that executes once.  Whether you use a DynamicResource in the XAML or not doesn't matter here, because a new bitmap creation never occurs again after the theme change, and there are no bindings set up to trigger any future updates.

If you manually run your same ImageSourceSmall updating logic again (calling XamlToImageConverter.Convert to create a new bitmap) right after the theme change, it updates the icon as you'd expect since the DynamicResource is picking up the updated brush and you have refreshed the bitmap.

Overall, the best way of doing vector icons in WPF is to use DrawingImage.  But those are tougher to create than other kinds of vector-based icons, such as with Paths.

You alternatively could bind your ImageSourceSmall to some string dependency property (where you'd pass your "CrossIcon" string) and use your XamlToImageConverter as the value converter on that binding.  It still wouldn't update after the theme change automatically though since you'd need to update that string dependency property to trigger WPF's value converter logic to kick in again.  But if you set the string property to null then back to "CrossIcon", it should run the value converter logic again.  


Actipro Software Support

The latest build of this product (v2019.1 build 0682) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.