更改“菜单”状态的最佳方法是WPF 中的用户控件

发布于 2024-10-10 04:08:12 字数 434 浏览 2 评论 0原文

我有一个 UserControl,我想要有 3 种可能的状态:(

-Main menu
-Config #1
-Config #2

所有三个都是带有各种按钮、文本框等的 UserControl,父 UserControl 的状态根据当前状态将子控件设置为可见/不可见。

)菜单状态有按钮可以转到两个配置状态之一(这两个状态都有一个“后退”按钮可以返回主菜单)。

实施此工作流程的“最简单”方法是什么?我是否需要手动进行视图更改并为每个按钮实现单击处理程序,从而更改父组件的状态? (我在 Blend 中执行此操作,因此我试图避免在 VS 之外编写 C# 代码。)

如果我当前的设计(带状态的用户控件)使事情变得比需要的更困难,我愿意接受替代方案建议。 (这不是一个网页,只是一个常规应用程序,带有一些小屏幕导航+主屏幕内的子屏幕导航。)

I have a UserControl which I want to have 3 possible states:

-Main menu
-Config #1
-Config #2

(All three are UserControls with various buttons, textboxes, etc., and the parent UserControl's states set the child controls to visible/not visible depending on the current state.)

The main menu state has buttons to go to either of the 2 config states (and both of those states have a 'back' button to return to the main menu).

What is the 'easiest' way to implement this workflow? Do I need to do view changes manually and implement a click handler for each button, changing the state of the parent component? (I'm doing this in Blend so I'm trying to avoid having to write C# code outside of VS.)

If my current design (UserControl w/states) makes things harder than it needs to be, I'm open to alternative suggestions. (This is not a webpage, just a regular application with some minor screen navigation + sub-screen navigation within major screens.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2024-10-17 04:08:12

您可以在 UserControl 上创建一个枚举(公开这三个值)属性,并使用 xaml 触发器来修改视图(参见下面的示例)。
为了根据菜单状态更改状态,您可以将所有菜单项单击事件处理为一个处理程序(或一个命令),传递适当的枚举作为命令参数或使用 Tag 属性,然后设置当前的 CurrentState 属性。
另一种方法是使用 StoryBoard 围绕各州进行循环。

示例(将 Window 替换为 UserControl,抱歉):

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
  }

  public CurrentState CurrentState
  {
    get { return (CurrentState)GetValue(CurrentStateProperty); }
    set { SetValue(CurrentStateProperty, value); }
  }
  public static readonly DependencyProperty CurrentStateProperty =
    DependencyProperty.Register("CurrentState", typeof(CurrentState),
      typeof(MainWindow), new UIPropertyMetadata(CurrentState.MainMenu));    
}

public enum CurrentState
{
  MainMenu,
  Config1,
  Config2
}

Xaml:

<Window 
  x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:WpfApplication1">
  <Window.Style>
    <Style TargetType="{x:Type src:MainWindow}">
      <Style.Triggers>
        <Trigger Property="CurrentState" Value="MainMenu">
          <Setter Property="Background" Value="Red" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Style>
</Window>

You could make an enum (that exposes these three values) property on your UserControl, and use xaml triggers to modify the view (look example bellow).
In order to change state according to menu state, you could address all the menuitems click event to one handler (or to one command) passing the appropriate enum as command parameter or with the Tag property, then set current CurrentState property.
Another way is using a StoryBoard to circle around the states.

Example (replace Window with UserControl, sorry):

public partial class MainWindow : Window
{
  public MainWindow()
  {
    InitializeComponent();
  }

  public CurrentState CurrentState
  {
    get { return (CurrentState)GetValue(CurrentStateProperty); }
    set { SetValue(CurrentStateProperty, value); }
  }
  public static readonly DependencyProperty CurrentStateProperty =
    DependencyProperty.Register("CurrentState", typeof(CurrentState),
      typeof(MainWindow), new UIPropertyMetadata(CurrentState.MainMenu));    
}

public enum CurrentState
{
  MainMenu,
  Config1,
  Config2
}

Xaml:

<Window 
  x:Class="WpfApplication1.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:src="clr-namespace:WpfApplication1">
  <Window.Style>
    <Style TargetType="{x:Type src:MainWindow}">
      <Style.Triggers>
        <Trigger Property="CurrentState" Value="MainMenu">
          <Setter Property="Background" Value="Red" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Style>
</Window>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文