选项卡控件与当前选项卡交互后冻结

时间:2017-10-25 13:07:58

标签: c# wpf mvvm tabs tabcontrol

我的标签控件中有三个标签,我可以自由点击,一切都按计划运行。但是,第二个我与当前选项卡交互,下一个选项卡更改将选项卡控件锁定到我无法更改选项卡并强制重新打开该程序的位置。我尝试过使用自定义选项卡控件,使用内容展示器和数据模板,但此选项卡控件仍然锁定。

通常情况如下:

enter image description here

这是冻结时的样子:

enter image description here

这是视图中的标签控件:

<controls:ViewModelToViewConverter x:Key="ViewViewModelConverter"/>
...

<TabControl SelectedIndex="{Binding SelectTabIndex, Mode=TwoWay}" >
  <TabItem x:Name="ApInvoicesTab" Header="AP Invoices" > 
     <ContentControl Content="{Binding CurrentViewModel, Converter={StaticResource ViewViewModelConverter}}"/>
   </TabItem>
   <TabItem x:Name="ArInvoicesTab" Header="AR Invoices" >
     <ContentControl Content="{Binding CurrentViewModel, Converter={StaticResource ViewViewModelConverter}}"/>
   </TabItem>
   <TabItem x:Name="HistoryTab" Header="History"> 
     <ContentControl Content="{Binding CurrentViewModel, Converter={StaticResource ViewViewModelConverter}}"/>
   </TabItem>
 </TabControl>

转换器:

public class ViewModelToViewConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null) return null;

        //use naming convention or custom settings here to get view type
        var viewModelType = value.GetType();
        var viewType = Type.GetType(value.ToString().Replace("Model", ""));

        var view = (FrameworkElement)Activator.CreateInstance(viewType);
        view.DataContext = value;
        return view;
    }
}

主视图模型(带有选项卡的主视图的datacontext):

    //private properties
    private int _selectTabIndex;
    private ViewModelBase _currentViewModel;
    private List<ViewModelBase> _viewModels;


    //Constructor
    public MainViewModel()
    {
        try
        {
            _viewModels = new List<ViewModelBase>
            {
                new ApInvoiceViewModel(),
                new ArInvoiceViewModel(),
                new InvoiceHistoryViewModel()
            };

            PropertyChanged += MainViewModel_PropertyChanged;

            CurrentViewModel = _viewModels[0];
                ((IRefreshable)CurrentViewModel).RefreshData();
        }
        catch (Exception e)
        {

        }
    }

    //Command that displays correct viewmodel
    public RelayCommand<object> SetCurrentViewModelCommand
    {
        get
        {
            return _setCurrentViewModelCommand ?? (_setCurrentViewModelCommand = new RelayCommand<object>(
                      (getType) =>
                      {
                          try
                          {
                              if (getType != null)
                              {
                                  _messageService.ShowWorkingMessage();

                                  CurrentViewModel = _viewModels[(int)getType];

                                  if (CurrentViewModel is IRefreshable)
                                      ((IRefreshable)CurrentViewModel).RefreshData();

                                  _messageService.HideWorkingMessage();
                              }
                              IsLoading = false;
                          }
                          catch (Exception e)
                          {
                              IntegratorJobDAL.SetIntegratorJobError(0, e.ToString(), GetType().Name);
                              _messageService.ShowErrorMessage(e.Message);
                          }
                      }
                      ));
        }

    }


    //public properties and propertychanged
    private void MainViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName.Equals("SelectTabIndex"))
        {
            SetCurrentViewModelCommand.Execute(SelectTabIndex);
        }
    }

    public int SelectTabIndex
    {
        get { return _selectTabIndex; }
        set { Set("SelectTabIndex", ref _selectTabIndex, value); }
    }

    public ViewModelBase CurrentViewModel
    {
        get { return _currentViewModel; }
        set { Set("CurrentViewModel", ref _currentViewModel, value); }
    }

0 个答案:

没有答案