Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF中的styling按钮_Wpf_Xaml_Styles - Fatal编程技术网

WPF中的styling按钮

WPF中的styling按钮,wpf,xaml,styles,Wpf,Xaml,Styles,我有一个样式用于按钮,如下代码所示 <Style TargetType="Button" x:Key="TransparentButton"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border HorizontalAlignment="

我有一个样式用于
按钮
,如下代码所示

<Style TargetType="Button" x:Key="TransparentButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border HorizontalAlignment="Center" x:Name="borderTemplate"
                        Background="Transparent">
                    <ContentPresenter/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="borderTemplate"
                                Property="Border.BorderBrush" Value="Gray" />
                        <Setter TargetName="borderTemplate"
                                Property="Border.BorderThickness" Value="1" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="true">
                        <Setter TargetName="borderTemplate"
                                Property="Border.BorderBrush" Value="Lime" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
应用上述样式后,按钮将看起来与下图一样。

 <Button Background="Black" Style="{StaticResource TransparentButton}"
         Content="0123456789" Height="15" HorizontalAlignment="Left"
         Name="button2" VerticalAlignment="Top" Margin="70,17,0,0" />

这可以通过使用RadioButton而不是Button来完成 看看这个

<Style x:Key="RadioButtonStyle1" TargetType="{x:Type RadioButton}">
    <Setter Property="Background" Value="#F4F4F4"/>
    <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type RadioButton}">
                 <Border  HorizontalAlignment="Center" x:Name="borderTemplate" Background="Transparent" VerticalAlignment="Center">
                            <ContentPresenter x:Name="contentPresenter" VerticalAlignment="Center"/>
                        </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" TargetName="borderTemplate" Value="#FFE4E4E4"/>
                        <Setter Property="HorizontalAlignment" TargetName="contentPresenter" Value="Center"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Background" TargetName="borderTemplate" Value="#FFA1A1A1"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Grid x:Name="LayoutRoot">
    <StackPanel/>
    <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,86.04,0,0" Style="{StaticResource RadioButtonStyle1}"/>
    <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="116,106,0,0" Style="{StaticResource RadioButtonStyle1}"/>
</Grid>


感谢您给出答案……我已经尝试过这个代码……但它的结果是……只要我按下按钮上的鼠标……它就会将
边框设置为石灰,将
背景设置为灰色……**当
鼠标点击事件发生时,应该是灰色的???
背景应该是灰色的……谢谢……。对于演示,您可以看到SilverLight的数据寻呼机控件…这里是..你可以看到那边..导航到那篇文章的演示部分..谢谢..我用RadioButton而不是按钮样式更新了我的答案请看一看..谢谢..回答..但是这里发生了什么变化..所有按钮都变成了
灰色
,除了..选中的一个。。。谢谢…非常感谢你的回答…但是。。。我找不到StaticResource
CheckBoxStroke
我已将其设置为…。尽管当鼠标按在按钮上时,边框未更改。。。。