无法设置3个切换按钮文本内容,使用相同的样式

时间:2017-07-23 22:46:06

标签: c# wpf xaml togglebutton

我有三个切换按钮,它们在wpf上使用单一样式资源,现在我无法设置其内容,内容被多次声明错误,每个切换按钮都有不同的内容。 每个切换按钮也使用图像作为背景。

<UserControl.Resources>
    <Style x:Key="Chromeless" TargetType="ToggleButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">

                    <Border BorderThickness="0" Width="197" Height="60">

                        <ContentPresenter TextElement.FontFamily="{TemplateBinding TextElement.FontFamily}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="0,0,0,0"/>
                    </Border>

                </ControlTemplate>

            </Setter.Value>
        </Setter>

    </Style>
    <Image  x:Key="page1Pressed" Source="/graph_engine;Component/cucaracha/LD3/button_1.png" Height="60" Width="197" />
    <Image  x:Key="page1" Source="/graph_engine;Component/cucaracha/LD3/button_1_pressed.png" Height="60" Width="197" />

    <Image  x:Key="page2Pressed" Source="/graph_engine;Component/cucaracha/LD3/button_2.png" Height="60" Width="197" />
    <Image  x:Key="page2" Source="/graph_engine;Component/cucaracha/LD3/button_2_pressed.png" Height="60" Width="197" />

    <Image  x:Key="page3Pressed" Source="/graph_engine;Component/cucaracha/LD3/button_3.png" Height="60" Width="197" />
    <Image  x:Key="page3" Source="/graph_engine;Component/cucaracha/LD3/button_3_pressed.png" Height="60" Width="197" />

</UserControl.Resources>
<Viewbox Stretch="Fill" StretchDirection="Both">
    <Grid Height="60" Name="grid1" Width="591" Margin="0,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="197" />
            <ColumnDefinition Width="197" />
            <ColumnDefinition Width="197" />
        </Grid.ColumnDefinitions>
        <ToggleButton  Style="{StaticResource Chromeless}"  Name="page1" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Foreground="{x:Null}" Checked="page1_Checked" Unchecked="page1_Unchecked">
            <DynamicResource ResourceKey="page1"/>
        </ToggleButton>
        <ToggleButton Grid.Column="1" Style="{StaticResource Chromeless}"  Name="page2" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Foreground="{x:Null}" Unchecked="page2_Unchecked" Checked="page2_Checked">
            <DynamicResource ResourceKey="page2"/>
        </ToggleButton>

        <ToggleButton Grid.Column="2" Style="{StaticResource Chromeless}"  Name="page3" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" Foreground="{x:Null}" Checked="page3_Checked" Unchecked="page3_Unchecked">
            <DynamicResource ResourceKey="page3"/>
        </ToggleButton>
    </Grid>
</Viewbox>

1 个答案:

答案 0 :(得分:0)

您是说在按下切换按钮时无法更改图像吗?您只需将图像设置为切换按钮的内容。您可以使用Visual State Manager更改背景,如this

如果您只想设置图像而失败。请检查图像路径。