WPF利用radiobutton製做菜單按鈕

原文: WPF利用radiobutton製做菜單按鈕

版權聲明:歡迎轉載。轉載請註明出處,謝謝 https://blog.csdn.net/wzcool273509239/article/details/56480631

實現功能:鼠標移入和選中高亮顯示,話很少說,直接上樣式。css

樣式:html

<!--========================================================RadioButton 樣式 通用====================================-->
    <Style x:Key="Tong_Yong_RadioButtonStyle" TargetType="{x:Type RadioButton}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <!--<Setter Property="Background" Value="#F4F4F4"/>
        <Setter Property="BorderBrush" Value="{StaticResource CheckBoxStroke}"/>-->
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Margin" Value="10,10,0,0"></Setter>
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="Height" Value="{Binding Height}"></Setter>
        <Setter Property="Width" Value="{Binding Width}"></Setter>
        <Setter Property="Cursor" Value="Hand"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Grid>
                        <!--選中或者鼠標移入顏色-->
                        <Grid x:Name="B" Visibility="Collapsed" Background="{TemplateBinding BorderBrush}"/>
                        <!--默認顏色-->
                        <Grid x:Name="A" Background="{TemplateBinding Background}"/>
                        <ContentPresenter x:Name="contentPresenter" RecognizesAccessKey="True" Visibility="Collapsed"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="Visibility" TargetName="A" Value="Collapsed"/>
                            <Setter Property="Visibility" TargetName="B" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <!--<Setter Property="Visibility" TargetName="contentPresenter" Value="Visible"/>-->
                            <Setter Property="Visibility" TargetName="A" Value="Collapsed"/>
                            <Setter Property="Visibility" TargetName="B" Value="Visible"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--========================================================RadioButton 樣式 通用====================================-->


引用實例:

<!--首頁-->
            <RadioButton Name="rbHome" Style="{DynamicResource Tong_Yong_RadioButtonStyle}" Width="192" Height="63" IsChecked="True" Checked="RadioButton_Checked">
                <RadioButton.Background>
                    <ImageBrush ImageSource="/Agriculture.CommonUserControl;component/Images/MenuImages/home-.png"/>
                </RadioButton.Background>
                <RadioButton.BorderBrush>
                    <ImageBrush ImageSource="/Agriculture.CommonUserControl;component/Images/MenuImages/home.png"/>
                </RadioButton.BorderBrush>
                <Grid>
                    <Grid.Background>
                        <ImageBrush ImageSource="/Agriculture.CommonUserControl;component/Images/MenuImages/home.png"/>
                    </Grid.Background>
                </Grid>
            </RadioButton>
相關文章
相關標籤/搜索