WPF ComboBox选择更改TabItem选择更改

时间:2013-03-15 09:44:23

标签: c# mvvm combobox tabs selecteditem

我在MVVM的标签项中有一个组合框。此选项卡可以在我的应用程序中多次创建(相同视图,相同视图模型但不同实例),因此我可以从一个选项卡切换到另一个选项卡(但它们是相同类型的选项卡)。

它与每个WPF控件完美配合,但使用组合框我有一个奇怪的行为: 焦点选项卡失去焦点时,会获取应用程序关注的选项卡的复选框的选定项目。

如果我从不同类型的2个标签切换一切正常,有什么想法吗?感谢。

XAML:

<CollectionViewSource x:Key="StatusView" Source="{Binding Path=StatusList}"/>
<ComboBox Name="_spl2Status" Grid.Column="3" Grid.Row="0"
          ItemsSource="{Binding Source={StaticResource StatusView}}"
          SelectedValue="{Binding Path=CurrentSPL2.ID_SPL2_STATUS, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          SelectedValuePath="FL_TYPE"
          DisplayMemberPath="ID_TYPE">
</ComboBox>

VM:

public List<NullableByteEnumType> StatusList
{
    get
    {
        return (SPC_SPL2.SPL2StatusCollection.Skip(1)).ToList();
    }
}

private SPC_SPL2 _currentSPL2 = null;

public SPC_SPL2 CurrentSPL2
{
    get
    {
        if (_currentSPL2== null)
            Controller.Execute(delegate(IResult result)
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>();
                parameters.Add("FL_ACTIVE", true);
                parameters.Add("ID_SPL2", _itemcode);
                Model.Invalidate(typeof(SPC_SPL2), Filter.GENERIC<SPC_SPL2>(parameters, "ID_SPL2"));
                Model.Include<SPC_SPL2>();

                if (Model.Appendload(result) == false)
                    return false;

                Debug.Assert(Context.SPC_SPL2.Count == 1);
                _currentSPL2= Context.SPC_SPL2.FirstOrDefault();

                return result.Successful;
            });

        return _currentSPL2;
    }
    set
    {
        _currentSPL2= value;
        OnPropertyChanged(() => CurrentSPL2);
    }
}

我的标签以这种方式处理:

<Grid>
    <Border Grid.Row="0">
        <ContentControl 
            Content="{Binding Path=Workspaces}" 
            ContentTemplate="{StaticResource MasterWorkspacesTemplate}"
            />
    </Border>
</Grid>

,其中

 <DataTemplate x:Key="MasterWorkspacesTemplate">
            <TabControl IsSynchronizedWithCurrentItem="True" 
                        BorderThickness="0" 
                        ItemsSource="{Binding}" 
                        SelectedItem="{Binding}"
                        ItemContainerStyleSelector="{StaticResource TabItemTemplate}"
                        />
        </DataTemplate>

和workspaces(我的viewmodels列表)(T是一个从viewModelBase继承的类)

 public T CurrentWorkspace
    {
        get { return WorkspacesView.CurrentItem as T; }
    }

    private ObservableCollection<T> _workspaces;
    public ObservableCollection<T> Workspaces
    {
        get
        {
            if (_workspaces == null)
            {
                _workspaces = new ObservableCollection<T>();
                _workspaces.CollectionChanged += _OnWorkspacesChanged;
            }

            return _workspaces;
        }
    }
    protected ICollectionView WorkspacesView
    {
        get
        {
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(Workspaces);
            Debug.Assert(collectionView != null);
            return collectionView;
        }
    }

2 个答案:

答案 0 :(得分:1)

我重新创建了你的问题。但我找不到任何问题。请看下面的代码,你可能会得到解决。这是我的解决方案。

MyTab查看模型

public class MyTab : ViewModelBase
{
    #region Declarations

    private ObservableCollection<string> statusList;
    private string selectedStatus;

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the header.
    /// </summary>
    /// <value>The header.</value>
    public string Header { get; set; }

    /// <summary>
    /// Gets or sets the content.
    /// </summary>
    /// <value>The content.</value>
    public string Content { get; set; }

    /// <summary>
    /// Gets or sets the status list.
    /// </summary>
    /// <value>The status list.</value>
    public ObservableCollection<string> StatusList
    {
        get
        {
            return statusList;
        }
        set
        {
            statusList = value;
            NotifyPropertyChanged("StatusList");
        }
    }

    /// <summary>
    /// Gets or sets the selected status.
    /// </summary>
    /// <value>The selected status.</value>
    public string SelectedStatus
    {
        get
        {
            return selectedStatus;
        }
        set
        {
            selectedStatus = value;
            NotifyPropertyChanged("SelectedStatus");
        }
    }

    #endregion
}

MainViewModel查看模型

public class MainViewModel : ViewModelBase
{
    #region Declarations

    private ObservableCollection<MyTab> tabs;
    private MyTab selectedTab;

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the tabs.
    /// </summary>
    /// <value>The tabs.</value>
    public ObservableCollection<MyTab> Tabs
    {
        get
        {
            return tabs;
        }
        set
        {
            tabs = value;
            NotifyPropertyChanged("Tabs");
        }
    }

    /// <summary>
    /// Gets or sets the selected tab.
    /// </summary>
    /// <value>The selected tab.</value>
    public MyTab SelectedTab
    {
        get
        {
            return selectedTab;
        }
        set
        {
            selectedTab = value;
            NotifyPropertyChanged("SelectedTab");
        }
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="MainViewModel"/> class.
    /// </summary>
    public MainViewModel()
    {
        this.Tabs = new ObservableCollection<MyTab>();

        MyTab tab1 = new MyTab();
        tab1.Header = "tab1";
        tab1.Content = "Tab 1 content";
        ObservableCollection<string> tab1StatusList = new ObservableCollection<string>();
        tab1StatusList.Add("tab1 item1");
        tab1StatusList.Add("tab1 item2");
        tab1StatusList.Add("tab1 item3");
        tab1.StatusList = tab1StatusList;
        tab1.SelectedStatus = tab1StatusList.First();
        this.Tabs.Add(tab1);

        MyTab tab2 = new MyTab();
        tab2.Header = "tab2";
        tab2.Content = "Tab 2 content";
        ObservableCollection<string> tab2StatusList = new ObservableCollection<string>();
        tab2StatusList.Add("tab2 item1");
        tab2StatusList.Add("tab2 item2");
        tab2StatusList.Add("tab2 item3");
        tab2.StatusList = tab2StatusList;
        tab2.SelectedStatus = tab2StatusList.First();
        this.Tabs.Add(tab2);

        this.SelectedTab = tab1;
    }

    #endregion
}

最后这是我的XAML

 <Window x:Class="ComboboxSelectedItem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:viewModel="clr-namespace:ComboboxSelectedItem.ViewModels"
    Title="MainWindow" Height="350" Width="525">
<Grid Name="mainGrid">

    <Grid.DataContext>
        <viewModel:MainViewModel />
    </Grid.DataContext>

    <TabControl
        ItemsSource="{Binding Tabs, Mode=TwoWay}"
        SelectedItem="{Binding SelectedTab}">

        <TabControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Header}" Margin="0 0 20 0"/>
                </StackPanel>
            </DataTemplate>
        </TabControl.ItemTemplate>

        <!--Content section-->
        <TabControl.ContentTemplate>
            <DataTemplate>
                <Grid>
                    <StackPanel Orientation="Vertical">
                        <TextBlock
                            Text="{Binding Content}" />
                        <ComboBox
                            ItemsSource="{Binding StatusList}"
                            SelectedItem="{Binding SelectedStatus}" />
                    </StackPanel>

                </Grid>
            </DataTemplate>
        </TabControl.ContentTemplate>

    </TabControl>
</Grid>
</Window>

答案 1 :(得分:0)

您是否完全确定要创建viewmodel的新实例。如果没有,则组合框共享相同的集合视图源,这意味着一个组合框的变化将反映在所有组合框中。我自己也有这个问题。

尝试在代码中声明集合视图源:

CollectionViewSource StatusListViewSource = new CollectionViewSource();
StatusListViewSource.Source = SPL2StatusCollection;

然后在xaml中更改绑定到collectionviewsource:

ItemsSource="{Binding StatusListViewSource.View}"

我从vb转换,因此可能需要进行一些编辑 这有帮助吗?