无法将图像显示为WPF ToggleButton的内容

无法将图像显示为WPF ToggleButton的内容,wpf,xaml,Wpf,Xaml,我现在正在与VS 2015和WPF合作。 几周前,我在项目的App.xaml中定义了ToggleButton的模板样式,以便用户可以查看按钮是否被选中。 最初,togglebutton的内容是带有文本的文本块。 现在我想用一个图像替换这个文本块。 此外,我在App.xaml中为图像定义了一些资源 以下是App.xaml: <Application x:Class="WPFDesignerPrototyp.App" xmlns="http://schemas.mic

我现在正在与VS 2015和WPF合作。 几周前,我在项目的App.xaml中定义了ToggleButton的模板样式,以便用户可以查看按钮是否被选中。 最初,togglebutton的内容是带有文本的文本块。 现在我想用一个图像替换这个文本块。 此外,我在App.xaml中为图像定义了一些资源

以下是App.xaml:

<Application x:Class="WPFDesignerPrototyp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPFDesignerPrototyp"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- Style for togglebuttons-->
        <Style x:Key="toggleButtonStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border x:Name="outer"
                                BorderBrush="Black"
                                BorderThickness="1"
                                Margin="5,5,0,0"
                                Opacity=".9"
                                Background="Transparent">
                            <Border x:Name="inner"
                                      Margin="5"
                                      BorderThickness="0"
                                      Background="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}">
                                <Grid x:Name="container">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="2*"/>
                                        <RowDefinition/>
                                    </Grid.RowDefinitions>
                                    <!--<TextBlock x:Name="display"
                                               Grid.Row="1"
                                               TextAlignment="Center"
                                               Text="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
                                               Margin="5,2,5,2"/>-->
                                    <Image x:Name="displayimage"
                                           Grid.Row="1"
                                           HorizontalAlignment="Center"
                                           Source="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"
                                           Margin="5,2,5,2"/>
                                </Grid>
                            </Border>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsChecked" Value="True">
                                <Setter TargetName="outer" Property="Background" Value="LightBlue"/>
                                <Setter TargetName="outer" Property="BorderBrush" Value="Transparent"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Image x:Key="imageHorizontalAlignmentLeft" Source="C:/Utilities/VS 2015 ImageLibrary/2015_VSIcon/AlignLeft/AlignLeft_16x.png"/>
        <Image x:Key="imageHorizontalAlignmentCenter" Source="C:/Utilities/VS 2015 ImageLibrary/2015_VSIcon/AlignStretchHorizontal/AlignStretchHorizontal_16x.png"/>
        <Image x:Key="imageHorizontalAlignmentRight" Source="C:/Utilities/VS 2015 ImageLibrary/2015_VSIcon/AlignRight/AlignRight_16x.png"/>
    </Application.Resources>
</Application>

然后,在我的WPF窗口中,我将新内容实现到我的一个切换按钮中

<ToggleButton x:Name="setAlignLeft" Grid.Row="1" Grid.Column="1" Content="{StaticResource imageHorizontalAlignmentLeft}" Style="{StaticResource toggleButtonStyle}" Click="setAlignLeft_Click"/>

但不幸的是什么也没发生。 按钮的内容为空。 所以我尝试了另一个togglebutton,没有样式toggleButtonStyle,结果它成功了

我想我的模板中一定有错误,但我不知道在哪里

有人能帮忙吗

提前感谢,,
Patrick

您将需要与
内容绑定。父级的源代码
,因为
内容
已经是图像

   <Image x:Name="displayimage"
   Grid.Row="1"
   HorizontalAlignment="Center"
   Source="{Binding Content.Source, RelativeSource={RelativeSource TemplatedParent}}"
   Margin="5,2,5,2"/>

谢谢你,尼廷!事情就是这样。