【问题标题】:how to open specific section in window on button click in wpf c#如何在wpf c#中单击按钮时在窗口中打开特定部分
【发布时间】:2021-05-04 06:38:16
【问题描述】:

我想在从另一个窗口单击按钮时打开一个窗口,然后转到该窗口中的特定选项卡。假设新窗口 (tabsWindow) 有 3 个选项卡 - tab1、tab2 和 tab3,我在主窗口 (buttonsWindow) btn1、btn2 和 btn3 上有 3 个按钮。所有三个选项卡都在一个窗口上,所有三个按钮都在另一个窗口上。单击 btn1 时,应从 tabsWindow 打开 tab1。在 btn2_Click 上,tab2 应该打开。我听说过 RoutedCommand 但并不擅长。建议我任何其他可能或更简单的方法。

【问题讨论】:

  • 我建议你改写你的问题。转到部分没有任何意义。 wpf 中没有节之类的东西。如果它们是标签,那么您需要选择一个标签。如果不是,那么关于标签的那些东西是什么。我不知道你想从你的描述中得到什么,所以我认为你的问题很可能会被解决。
  • 编辑了我的问题
  • 您的应用是如何构建的? Mvvm,因此视图模型是否会驱动选定项 - 例如,您是否在 tabswindow 中硬编码 3 选项卡项内容?
  • 是的选项卡项目是硬编码的。是的,它是 MVVM。
  • 因为你没有绑定 tabcontrol 的 itemssource。在你的 tabswindow 上添加一个方法需要一个 int。使用它来设置 tabcontrol 上的 selectedindex。从按钮传递。或者类似地在 tabswindow 上重载 ctor

标签: c# wpf routed-commands


【解决方案1】:

我使用 MVVM 模式制作了一个玩具样本。
您可以在 GitHub 中查看完整源代码。

ButtonsWindow.xaml

我使用Command,将Button对象交给CommandParameter来指定TabControl的SelectedIndex使用 Button 的内容。

<UniformGrid Columns="3">
    <Button Margin="50" Content="1" Command="{Binding BtnClick}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
    <Button Margin="50" Content="2" Command="{Binding BtnClick}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
    <Button Margin="50" Content="3" Command="{Binding BtnClick}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
</UniformGrid>

TabsWindow.xaml

<Grid>
    <TabControl x:Name="tab" Width="300" Height="300">
        <TabItem Header="Tab1">
            <TextBlock TextAlignment="Center" VerticalAlignment="Center" Text="Tab1" FontSize="20" />
        </TabItem>
        <TabItem Header="Tab2">
            <TextBlock TextAlignment="Center" VerticalAlignment="Center" Text="Tab2" FontSize="20" />
        </TabItem>
        <TabItem Header="Tab3">
            <TextBlock TextAlignment="Center" VerticalAlignment="Center" Text="Tab3" FontSize="20" />
        </TabItem>
    </TabControl>
</Grid>

TabsWindow.xaml.cs

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

    internal void SetTab(string content)
    {
        tab.SelectedIndex = int.Parse(content) - 1;
    }
}

MainViewModel.cs

public class MainViewModel
{
    private TabsWindow win;
    public ICommand BtnClick { get; set; }

    public MainViewModel()
    {
        BtnClick = new RelayCommand<object>(Click);
    }

    private void Click(object obj)
    {
        if (obj is Button btn)
        {
            if (win is null || !win.IsVisible)
            { 
                win = new TabsWindow();
                win.Show();
            }
            win.SetTab(btn.Content.ToString());
        }
    }
}

当点击Button 时,TabsWindow 被激活并根据Button 的Content 移动到特定选项卡。此外,如果TabsWindow 已激活,则可以移动选项卡而不显示新的TabsWindow。

【讨论】:

  • 我已经尝试过这段代码,但它对我不起作用。
  • @akilesh 我又试了一次,但它有效。您是否尝试过 github 中的完整源代码?具体告诉我哪个部分不起作用。
  • 嘿@elena.kim 成功了。我从 github 获取了整个代码。谢谢
  • 我想即兴创作。你能帮我吗? @elena.kim
  • @akilesh 写一个新问题,然后把这个链接放在这个评论中怎么样?我去看看。
猜你喜欢
  • 2023-02-07
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
相关资源
最近更新 更多