如何绑定样式中的静态资源图像?

时间:2019-10-21 23:02:33

标签: c# wpf xaml

我有一种样式,可以在对勾标记和空白之间切换。我想使用我的StaticResource来显示图像,而不是图像资源文件的硬编码路径。问题是Image.Source不允许我使用静态资源,并显示错误“ InvalidCastException:无法将类型为“ System.Windows.Style”的对象强制转换为类型为“ System.Windows.Media.ImageSource”。好的,我也尝试将Image.Source更改为Image.Style,但这还会产生另一个错误“ ArgumentException:不允许Style对象影响其所应用的对象的Style属性。”我不太清楚这一点。任何帮助将不胜感激。

我确实确保我的images.xaml资源在App.xaml的合并资源列表中的styles.xaml资源上方。我相信这很重要。

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>

0 个答案:

没有答案