0

I have a style which toggles between a checkmark and a blank space. I'd like to use my StaticResource to display the images instead of a hardcoded path to my image resource file. The problem is that Image.Source doesn't allow me to use the static resource and gives the error "InvalidCastException: Unable to cast object of type 'System.Windows.Style' to type 'System.Windows.Media.ImageSource." OK, I also tried changing Image.Source to Image.Style but that also give another error "ArgumentException: Style object is not allowed to affect the Style property of the object to which it applies." I can't quite figure this one out. Any help would be appreciated.

I did make sure my images.xaml resource is above the styles.xaml resource in my merged resource list in App.xaml. I believe this matters.

Styles.xaml

    <Style x:Key="TMGrayCheckedRadioButton" TargetType="{x:Type RadioButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
    <Setter Property="Background" Value="{StaticResource GrayButtonBackground}"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Width" Value="{StaticResource BBWidth}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Border x:Name="border" Background="{TemplateBinding Background}" 
                            Grid.ColumnSpan="3"
                            BorderBrush="Black" BorderThickness="1"
                            CornerRadius="4">
                    </Border>
                    <ContentPresenter x:Name="contentPresenter"   
                        Grid.Column="1"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <Border x:Name="checked" 
                            Grid.ColumnSpan="3"
                            IsHitTestVisible="False" 
                            Background="Transparent" 
                            CornerRadius="4" />
                    <Image Margin="5" Height="{StaticResource fontSize}">
                        <Image.Style>
                            <Style>
                                <Setter Property="Image.Source" Value="/images/blank.png" />
                                <!--<Setter Property="Image.Style" Value="{StaticResource Blank}" />-->
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsChecked, 
                                RelativeSource={RelativeSource AncestorType=
                                {x:Type ToggleButton}}}" Value="True">
                                        <Setter Property="Image.Source" Value="/images/checked.png" />
                                        <!--<Setter Property="Image.Source" Value="{StaticResource Checked}" />-->
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                    <Image Margin="5" Grid.Column="2" Height="{StaticResource fontSize}">
                        <Image.Style>
                            <Style>
                                <Setter Property="Image.Source" Value="/images/blank.png" />
                                <!--<Setter Property="Image.Source"  Value="{StaticResource Blank}" />-->
                            </Style>
                        </Image.Style>
                    </Image>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource LightGrayButtonBackground}"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <!--<Setter Property="Foreground" Value="White"/>-->
                        <Setter Property="Background" Value="{StaticResource DarkButtonBackground}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

App.xaml

<Application x:Class="TMUI.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:TMUI"
         StartupUri="Views/SplashWindowView.xaml">
<!--  <StartupUri="MainWindow.xaml"> -->
<!-- Note: StartupUri is local to App.xaml -->

<Application.Resources>     
    <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>

            <!-- Load Infrastructure's Resource Dictionaries -->
            <ResourceDictionary Source="Resources\Fonts.xaml" />
            <ResourceDictionary Source="Resources\Images.xaml" />
            <ResourceDictionary Source="Resources\Styles.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Chart2DControl;component/Resources/Controls.xaml"/>
            <ResourceDictionary Source="pack://application:,,,/Chart2DControl;component/Resources/ColorsAndBrushes.xaml"/>

        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>

namg_engr
  • 319
  • 1
  • 12
  • 1
    https://stackoverflow.com/questions/347614/wpf-image-resources – geometrikal Oct 22 '19 at 01:05
  • 1
    Setting the Style property of an Image inside the Style of an Image is obviously wrong. Make sure that `Blank` is a resource of a type derived from ImageSource, e.g. BitmapImage, then assign it to the Source property. – Clemens Oct 22 '19 at 08:00

0 Answers0