如何在WPF中从一个tabitem移动到另一个tabitem [英] how to move from one tabitem to another in WPF

查看:549
本文介绍了如何在WPF中从一个tabitem移动到另一个tabitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有三个标签项的标签控件。每个标签项都有一个数据网格。

以及它们各自标签项上的所有这三个数据网格都是Master-Detail-SubDetail形式。



当用户在主数据网格上选择一行时,如何从第一个标签项导航到第二个标签项?我使用ADO.Net实体框架创建了一个模型来创建这个主 - 详细视图。

I have a tab-control with three tab items. each tab item has a datagrid placed on it.
and all these three datagrid''s on their respective tab items is of Master-Detail-SubDetail form.

how to navigate from first tab item''s to the second tab item'' when a user selects a row on the master datagrid ? I have created a Model using ADO.Net entity framework to create this master-detail view.

推荐答案

我有一个ObservableCollection,我将绑定到ItemsSource,我有标签的属性类型,我将绑定到SelectedValue。每当我想要更改选定的选项卡时,我只需设置所选的值。这是我的脏代码。我希望这可以帮助您解决问题。

I have an ObservableCollection which I would bind to the ItemsSource and I have a property type of Tag which I will bind to the SelectedValue. Whenever I want to change the selected tab, I just set the selected value. Here''s my dirty code. I hope this will help you with your problem.
<Window x:class="WpfApplication9.MainWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication9"
        Title="MainWindow" Height="350" Width="525">
    <stackpanel>
        <tabcontrol itemssource="{Binding TagCollection}" selectedvalue="{Binding SelectedTag}">
            <tabcontrol.itemtemplate>
                <datatemplate>                    
                    <textblock text="{Binding Name}" />                    
                </datatemplate>
            </tabcontrol.itemtemplate>
        </tabcontrol>
        <button content="Test" command="{Binding ClickCommand}" />
    </stackpanel>
</window>



这是我设置为视图数据上下文的视图模型。

我创建了一个单击commant,当你单击按钮时会触发它。这样您就可以将焦点设置为TagCollection [1](在基于0的索引中,它是第二个选项卡)。现在这里棘手的部分是你必须在setter上调用raisepropertychange方法。这将告诉视图有人为SelectedTag设置了新值,视图应该更新。


Here''s the view model which I set to the data context of the view.
I created a click commant which will be triggered when you click the button. This will let you set the focus to TagCollection[1] (in a 0 based index it''s the 2nd tab). Now the tricky part here is that you have to call raisepropertychange method on the setter. This will tell the view that someone set a new value for the SelectedTag and the view should update.

public class TagVM: ViewModelBase
{
    ObservableCollection<tag> tagCollection;
    public TagVM()
    {
        tagCollection = new ObservableCollection<tag>();
        tagCollection.Add(new Tag() { Name = "two", Priority = "2" });
        tagCollection.Add(new Tag() { Name = "another 2", Priority = "2" });
        tagCollection.Add(new Tag() { Name = "three", Priority = "3" });
        tagCollection.Add(new Tag() { Name = "one", Priority = "1" });
    }

    private Tag selectedTag;

    public Tag SelectedTag
    {
        get { return selectedTag; }
        set
        {
            selectedTag = value;
            base.RaisePropertyChanged("SelectedTag");
        }
    }

    public ObservableCollection<tag> TagCollection
    {
        get { return tagCollection; }
        set
        {
            tagCollection = value;
            base.RaisePropertyChanged("TagCollection");
        }
    }

    public ICommand ClickCommand
    {
        get
        {
            return new RelayCommand(() => { SelectedTag = TagCollection[1]; });
        }
    }
}


这篇关于如何在WPF中从一个tabitem移动到另一个tabitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆