简体   繁体   中英

how to open specific section in window on button click in wpf c#

I want to open a window on button click from another window and go to specific tab in that window. Let's say new window (tabsWindow) has 3 tabs - tab1, tab2 and tab3 and I have 3 buttons on main window (buttonsWindow) btn1, btn2 and btn3. All three tabs are on one window and all three buttons are on another window. On click of btn1, tab1 should be opened from tabsWindow. On btn2_Click, tab2 should open. I have heard of RoutedCommand but not good in it. Suggest me any other possible or simpler way.

I made a toy sample by using MVVM pattern.
You can see full source in GitHub .

ButtonsWindow.xaml

I used Command and handed over the Button object to the CommandParameter to specify the SelectedIndex of TabControl using the Content of the 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());
        }
    }
}

When Button is clicked, TabsWindow is activated and moves to a specific tab according to the Content of the Button . Also, if TabsWindow is already activated, tab can be moved without showing a new TabsWindow .

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM