我怎样才能使WPF扩展器的行为像一个单选按钮?

pkwftd7m  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个侧菜单,上面有一些扩展器,我希望我的用户一次只选择一个扩展器,我试图设计扩展器控件,并将其基于切换按钮的单选按钮,但没有成功,我在谷歌上搜索了很多,没有发现任何东西,有人可以帮助吗,
下面是我所有的XAML代码,我的项目后面没有代码:

<Window x:Class="MyWPFApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:fa="http://schemas.awesome.incremented/wpf/xaml/fontawesome.sharp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen" Background="#7f7cf1">

<Window.Resources>

    <!--MenuRadioButtons Style-->
    <Style x:Key="menuRadioButtonStyle" BasedOn="{StaticResource {x:Type ToggleButton}}" TargetType="RadioButton">
        <Setter Property="Foreground" Value="#707db2"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Margin" Value="5 0 0 0"/>
        <Setter Property="Height" Value="45"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Tag" Value="#6673b7"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="RadioButton">
                    <Border Background="{TemplateBinding Background}" BorderThickness="4 0 0 0" BorderBrush="Transparent">
                        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="#bccaf1"/>
                <Setter Property="Foreground" Value="#bccaf1"/>
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Foreground" Value="#bccaf1"/>
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1, 0.5">
                            <GradientStop Color="#31407b" Offset="0"/>
                            <GradientStop Color="#495385" Offset="3"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="RadioButton">
                            <Border Background="{TemplateBinding Background}" BorderThickness="4 0 0 0" BorderBrush="#a5a1f5">
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>

        </Style.Triggers>
    </Style>

    <!--MenuRadioButtonIcons Style-->
    <Style x:Key="menuRadioButtonIconStyle" TargetType="fa:IconImage">
        <Setter Property="Width" Value="23"/>
        <Setter Property="Height" Value="23"/>
        <Setter Property="Margin" Value="5 0 0 0"/>
        <Setter Property="Foreground" Value="{Binding Path=Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RadioButton}}}"/>
    </Style>

    <!--MenuRadioButtonTextBlock Style-->
    <Style x:Key="menuRadioButtonTextBlockStyle" TargetType="TextBlock">
        <Setter Property="FontSize" Value="13"/>
        <Setter Property="Margin" Value="10 0 0 0"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RadioButton}}}"/>
    </Style>

    
    
    <!--MenuButtons Style-->
    <Style x:Key="menuButtonStyle" TargetType="Button">
        <Setter Property="Foreground" Value="#707db2"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Margin" Value="5 0 0 0"/>
        <Setter Property="Height" Value="45"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Tag" Value="#6673b7"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="{TemplateBinding Background}" BorderThickness="4 0 0 0" BorderBrush="Transparent">
                        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="#bccaf1"/>
                <Setter Property="Foreground" Value="#bccaf1"/>
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1, 0.5">
                            <GradientStop Color="#31407b" Offset="0"/>
                            <GradientStop Color="#495385" Offset="3"/>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Background="{TemplateBinding Background}" BorderThickness="4 0 0 0" BorderBrush="#a5a1f5">
                                <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <!--MenuButtonIcons Style-->
    <Style x:Key="menuButtonIconStyle" TargetType="fa:IconImage">
        <Setter Property="Width" Value="23"/>
        <Setter Property="Height" Value="23"/>
        <Setter Property="Margin" Value="5 0 0 0"/>
        <Setter Property="Foreground" Value="{Binding Path=Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
    </Style>
    
    <!--MenuButtonTextBlock Style-->
    <Style x:Key="menuButtonTextBlockStyle" TargetType="TextBlock">
        <Setter Property="FontSize" Value="13"/>
        <Setter Property="Margin" Value="10 0 0 0"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
    </Style>
    
    <!--ExpanderStyle1-->
    <Style x:Key="ExpanderHeaderFocusVisual1">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle Margin="0" StrokeDashArray="1 2" Stroke="Black" SnapsToDevicePixels="true" StrokeThickness="1"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ExpanderDownHeaderStyle1" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border Padding="{TemplateBinding Padding}">
                        <RadioButton Style="{StaticResource menuRadioButtonStyle}" HorizontalContentAlignment="Left">
                            <Grid Background="Transparent" SnapsToDevicePixels="False" Width="{Binding Path=Width, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RadioButton}}}">
                                <Ellipse x:Name="circle" Margin="200 0 0 0" Grid.Column="2" Fill="Transparent" HorizontalAlignment="Center" Height="19" Stroke="Transparent" VerticalAlignment="Center" Width="19"/>
                                <Path x:Name="arrow" Margin="200 0 0 0" Grid.Column="2" Data="M 1,1.5 L 4.5,5 L 8,1.5" HorizontalAlignment="Center" Stroke="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type RadioButton}}}" SnapsToDevicePixels="false" StrokeThickness="2" VerticalAlignment="Center"/>
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" Margin="4,0,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                            </Grid>
                        </RadioButton>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Data" TargetName="arrow" Value="M 1,4.5  L 4.5,1  L 8,4.5"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Stroke" TargetName="arrow" Value="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type RadioButton}}}"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Stroke" TargetName="arrow" Value="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type RadioButton}}}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Stroke" TargetName="arrow" Value="{Binding Path=Foreground, ElementName=BtnHome}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ExpanderStyle1" TargetType="{x:Type Expander}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="VerticalContentAlignment" Value="Stretch"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Expander}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="3" SnapsToDevicePixels="true">
                        <DockPanel>
                            <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" DockPanel.Dock="Top" FontStretch="{TemplateBinding FontStretch}" Foreground="{TemplateBinding Foreground}" FocusVisualStyle="{StaticResource ExpanderHeaderFocusVisual1}" FontStyle="{TemplateBinding FontStyle}" FontFamily="{TemplateBinding FontFamily}" FontWeight="{TemplateBinding FontWeight}" FontSize="{TemplateBinding FontSize}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource Mode=TemplatedParent}}" Margin="-0.8 0 0 0" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" Style="{StaticResource ExpanderDownHeaderStyle1}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Visibility="Collapsed"/>
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsExpanded" Value="true">
                            <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="250"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
     
    <!--Left Menu-->
    <Border x:Name="RightMenuBorder" CornerRadius="10 0 0 10" Grid.Column="0">
        <Border.Background>
            <LinearGradientBrush StartPoint="0, 0.5" EndPoint="1, 0.5">
                <GradientStop Color="#27396b" Offset="0"/>
                <GradientStop Color="#223266" Offset="2"/>
            </LinearGradientBrush>
        </Border.Background>
        <StackPanel>
            <!--Drop Down Side Menu-->

            <RadioButton Style="{StaticResource menuRadioButtonStyle}">
                <StackPanel Orientation="Horizontal">
                    <fa:IconImage Icon="Home" Style="{StaticResource menuRadioButtonIconStyle}"/>
                    <TextBlock Text="Home" Style="{StaticResource menuRadioButtonTextBlockStyle}"/>
                </StackPanel>
            </RadioButton>
            <RadioButton Style="{StaticResource menuRadioButtonStyle}">
                <StackPanel Orientation="Horizontal">
                    <fa:IconImage Icon="Home" Style="{StaticResource menuRadioButtonIconStyle}"/>
                    <TextBlock Text="Home" Style="{StaticResource menuRadioButtonTextBlockStyle}"/>
                </StackPanel>
            </RadioButton>
            <RadioButton Style="{StaticResource menuRadioButtonStyle}">
                <StackPanel Orientation="Horizontal">
                    <fa:IconImage Icon="Home" Style="{StaticResource menuRadioButtonIconStyle}"/>
                    <TextBlock Text="Home" Style="{StaticResource menuRadioButtonTextBlockStyle}"/>
                </StackPanel>
            </RadioButton>
            <Expander Style="{DynamicResource ExpanderStyle1}" Background="Transparent">
                <Expander.Header>
                    <StackPanel Orientation="Horizontal" Margin="-5 0 0 0">
                        <fa:IconImage Icon="DollarSign" Style="{StaticResource menuRadioButtonIconStyle}"/>
                        <TextBlock Text="Sales" Style="{StaticResource menuRadioButtonTextBlockStyle}"/>
                    </StackPanel>
                </Expander.Header>
                <StackPanel>
                    <Button Style="{StaticResource menuButtonStyle}">
                        <TextBlock Text="New Invoice" Style="{StaticResource menuButtonTextBlockStyle}" Margin="62 0 0 0"/>
                    </Button>
                    <Button Style="{StaticResource menuButtonStyle}">
                        <TextBlock Text="Manage Invoices" Style="{StaticResource menuButtonTextBlockStyle}" Margin="62 0 0 0"/>
                    </Button>
                </StackPanel>
            </Expander>
            <Expander Style="{DynamicResource ExpanderStyle1}" Background="Transparent">
                <Expander.Header>
                    <StackPanel Orientation="Horizontal" Margin="-5 0 0 0">
                        <fa:IconImage Icon="DollarSign" Style="{StaticResource menuRadioButtonIconStyle}"/>
                        <TextBlock Text="Sales" Style="{StaticResource menuRadioButtonTextBlockStyle}"/>
                    </StackPanel>
                </Expander.Header>
                <StackPanel>
                    <Button Style="{StaticResource menuButtonStyle}">
                        <TextBlock Text="New Invoice" Style="{StaticResource menuButtonTextBlockStyle}" Margin="62 0 0 0"/>
                    </Button>
                    <Button Style="{StaticResource menuButtonStyle}">
                        <TextBlock Text="Manage Invoices" Style="{StaticResource menuButtonTextBlockStyle}" Margin="62 0 0 0"/>
                    </Button>
                </StackPanel>
            </Expander>
            <Expander Style="{DynamicResource ExpanderStyle1}" Background="Transparent">
                <Expander.Header>
                    <StackPanel Orientation="Horizontal" Margin="-5 0 0 0">
                        <fa:IconImage Icon="DollarSign" Style="{StaticResource menuRadioButtonIconStyle}"/>
                        <TextBlock Text="Sales" Style="{StaticResource menuRadioButtonTextBlockStyle}"/>
                    </StackPanel>
                </Expander.Header>
                <StackPanel>
                    <Button Style="{StaticResource menuButtonStyle}">
                        <TextBlock Text="New Invoice" Style="{StaticResource menuButtonTextBlockStyle}" Margin="62 0 0 0"/>
                    </Button>
                    <Button Style="{StaticResource menuButtonStyle}">
                        <TextBlock Text="Manage Invoices" Style="{StaticResource menuButtonTextBlockStyle}" Margin="62 0 0 0"/>
                    </Button>
                </StackPanel>
            </Expander>
        </StackPanel>
    </Border>
    
</Grid>
vm0i2vca

vm0i2vca1#

这是你可以做的一种方式。每个Expander都必须使用Expanded事件处理程序才能使其工作。当一个Expander被展开时,所有其他Expander都将被折叠。
XAML:

<StackPanel>
    <Expander Expanded="Expander_Expanded"></Expander>
    <Expander Expanded="Expander_Expanded"></Expander>
    <Expander Expanded="Expander_Expanded"></Expander>
</StackPanel>

后面的代码:

private void Expander_Expanded(object sender, RoutedEventArgs e)
{
    var stackPanel = (sender as Expander).Parent as StackPanel;
    foreach(var child in stackPanel.Children)
    {
        if(child is Expander && child != sender)
        {
            ((Expander)child).IsExpanded = false;
        }
    }
}

相关问题