切换选项卡时,不会加载UserControls / ViewModels

时间:2013-11-23 16:58:34

标签: c# wpf mvvm user-controls tabcontrol

我是MVVM和WPF的新手。我正在尝试构建一个tabcontrol,其中的tabpages显示为usercontrols,当我在选项卡之间切换时,我无法找到用户控件未加载的原因。

public partial class WndMain : Window
{
    public WndMain()
    {
        InitializeComponent();

        var ViewModelWndMain = new ViewModelWndMain();

        this.DataContext = ViewModelWndMain;
    }
}

每个tabItem都有自己的ViewModel:

<Window x:Class="EasyBulking.WndMain"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ViewModels="clr-namespace:EasyBulking.ViewModels"
    xmlns:Tabs="clr-namespace:EasyBulking.GUI"
    Title="WndMain" Height="350" Width="525">
<Grid>
    <TabControl x:Name="TabsWndMain"
        ItemsSource="{Binding Tabs}"
        SelectedItem="{Binding SelectedTab, Mode=TwoWay}">

        <TabControl.Resources>                
            <DataTemplate DataType="{x:Type ViewModels:ViewModelTabProfile}">
                <Tabs:TabProfile />
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModels:ViewModelTabNutrition}">
                <Tabs:TabNutrition />
            </DataTemplate>
            <DataTemplate DataType="{x:Type ViewModels:ViewModelTabTraining}">
                <Tabs:TabTraining />
            </DataTemplate>              
        </TabControl.Resources>

        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding tabName}" />
            </Style>
        </TabControl.ItemContainerStyle>

    </TabControl>
</Grid>

相应的ViewModel:

class ViewModelWndMain : ViewModelBase
{        
    private ObservableCollection<ViewModelTab> tabs = new ObservableCollection<ViewModelTab>();
    private ViewModelTab selectedTab;
    private ResourceManager resourceManager { get; set; }

    public ViewModelWndMain ()
    {
        resourceManager = new ResourceManager("EasyBulking.Properties.Resources", Assembly.GetExecutingAssembly());

        Tabs.Add(new ViewModelTabProfile(resourceManager.GetString("tabProfile")));
        Tabs.Add(new ViewModelTabProfile(resourceManager.GetString("tabNutrition")));
        Tabs.Add(new ViewModelTabProfile(resourceManager.GetString("tabTraining")));

        SelectedTab = Tabs[0];
    }

    public ObservableCollection<ViewModelTab> Tabs
    {
        get
        {
            return tabs;
        }
    }

    public ViewModelTab SelectedTab
    {
        get { return selectedTab;   }
        set { 
            selectedTab = value;
            this.RaisePropertyChangedEvent("SelectedTab");               
        }
    }
}

当我切换标签时,ProperyChanged事件会触发,但UI不会更新。

abstract class ViewModelBase : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChangedEvent(string propertyName)
    {         
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

1 个答案:

答案 0 :(得分:0)

好的,只是在常规答案上关闭这个问题,我们走了:

添加选项卡'viewmodels的代码会为所有三个标签页插入相同的对象:

    Tabs.Add(new ViewModelTabProfile(resourceManager.GetString("tabProfile")));
    Tabs.Add(new ViewModelTabProfile(resourceManager.GetString("tabNutrition")));
    Tabs.Add(new ViewModelTabProfile(resourceManager.GetString("tabTraining")));

资源加载的标题文本掩盖了这一事实,并且看起来页面没有交换,实际上它们又从一个控件交换到同一类型的控件。