【问题标题】:WPF project, navigation with button's side menuWPF 项目,使用按钮的侧边菜单导航
【发布时间】:2017-06-02 12:14:06
【问题描述】:

我正在发现 WPF 和 MVVM,我认为我的问题对我接下来几天的项目工作很有帮助。

这就是我所拥有的:

visual

所以:

  • 每个按钮在窗口右侧打开一个视图
  • 活动按钮必须应用不同的样式(如绿色背景)

要加载页面 onClick 一个按钮,我不担心,我会在我的网格 onClick 上添加一个特定的 XAML。

但是要在活动按钮上应用不同的样式,我迷路了。 我是否设置了一个变量 onClick 并以我的风格 Resource 将触发器绑定到该变量?我不知道该怎么做。

我的 MainWindow 是这样定义的:

<Window x:Class="XXXX.UserInterface.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:XXXX.UserInterface"
        mc:Ignorable="d"
        Title="MainWindow"
        Style="{StaticResource CustomWindowStyle}">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="170"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Border Style="{StaticResource SideMenuBorder}">
            <StackPanel Grid.Column="0" Orientation="Vertical">
                <Button Click="ButtonCovertC_Click" 
                        x:Name="ButtonCovertC" 
                        Margin="5.5 9 8.5 0" 
                        Style="{StaticResource ButtonSideMenu}" 
                        Content="{local:Loc ButtonCovertC}"/>
                <Button Click="ButtonEarpieceC_Click" 
                        x:Name="ButtonEarpieceC" 
                        Margin="5.5 14 8.5 0" 
                        Style="{StaticResource ButtonSideMenu}" 
                        Content="{local:Loc ButtonEarpieceC}"/>
                <Button Click="ButtonRemoteC_Click" 
                        x:Name="ButtonRemoteC" 
                        Margin="5.5 14 8.5 0" 
                        Style="{StaticResource ButtonSideMenu}" 
                        Content="{local:Loc ButtonRemoteC}"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>

作为记录,我的按钮的样式资源:

<Style x:Key="ButtonSideMenu" TargetType="Button">
    <Setter Property="TextBlock.TextAlignment" Value="Center" />
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Height" Value="92"/>
    <Setter Property="Background" Value="{StaticResource ColorButtonBackground}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="{StaticResource ControlCornerRadius}" Background="{TemplateBinding Background}">

                    <ContentPresenter VerticalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="Button.IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="FontWeight" Value="DemiBold"/>
                        <Setter Property="Background" Value="{StaticResource ColorLayoutLine}"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="FontWeight" Value="Bold"/>
                        <Setter Property="Background" Value="{StaticResource ColorBoldSelectedButton}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

还有我的 MainWindow.xaml.cs:

namespace XXXX.UserInterface
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonCovertC_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ButtonEarpieceC_Click(object sender, RoutedEventArgs e)
        {


        }

        private void ButtonRemoteC_Click(object sender, RoutedEventArgs e)
        {


        }
    }
}

【问题讨论】:

    标签: c# wpf mvvm navigation


    【解决方案1】:

    使用样式为切换按钮而不是常规按钮的单选按钮。然后,您可以使用样式中的 IsChecked 属性来更改按钮的背景。这样,当一个按钮被选中时,背景颜色将保持不变。由于您使用了单选按钮,因此一次只能选中 1 个按钮。

    这是一个非常简单且丑陋(就样式而言)的示例。

    <Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="MyStyle"
               BasedOn="{StaticResource {x:Type ToggleButton}}"
               TargetType="RadioButton" >
               <Setter Property="Background" Value="Blue"/>
               <Setter Property="Width" Value="60"/>
               <Setter Property="Height" Value="40"/>
               <Setter Property="BorderBrush" Value="Black"/>
               <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type RadioButton}">
                        <Border Height="{TemplateBinding Height}"
                                Width="{TemplateBinding Width}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}">
                            <ContentPresenter VerticalAlignment="Center"
                                              HorizontalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter Property="Background" Value="Red"></Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <RadioButton Content="RadioButton"
                     HorizontalAlignment="Left"
                     Margin="117,123,0,0"
                     VerticalAlignment="Top"
                     Style="{StaticResource MyStyle}" />
        <RadioButton Content="RadioButton"
                     HorizontalAlignment="Left"
                     Margin="117,47,0,0"
                     VerticalAlignment="Top"
                     Style="{StaticResource MyStyle}" />
    
    </Grid>
    </Window>
    

    【讨论】:

    • 那是完美的。我正前往一个模型以获得我的窗口状态和一个视图模型来控制所有这些。关于您的解决方案有点矫枉过正。谢谢!
    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 2013-01-06
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2014-03-13
    • 2014-07-22
    相关资源
    最近更新 更多