c# - WPF ComboBox选择更改,TabItem选择更改

标签 c# mvvm combobox tabs selecteditem

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

它与每个WPF控件完美配合,但是对于组合框,我有一个奇怪的行为:
焦点选项卡失去焦点时,将获得应用程序所关注的选项卡的combox框中的选定项。

如果我从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>

虚拟机:
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>

和工作空间(我的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;
        }
    }

最佳答案

我已经解决了您的问题。但是我找不到任何问题。请查看下面的代码,您可能会获得解决方案。这是我的解决方案。
MyTab View 模型

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 View 模型
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>

关于c# - WPF ComboBox选择更改,TabItem选择更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429039/

相关文章:

wpf - WPF数据网格中行的背景颜色

wpf - 组合框 TextWrap 绑定(bind)

c# - 查询 Access DB 时条件表达式中的数据类型不匹配

c# - 如何为控制台和 WPF .NET 应用程序的 PythonNET 设置 Runtime.PythonDLL(PIP 安装)

xaml - MVVM - 绑定(bind)到深度嵌套字符串时的基础知识

c# - 将 UWP ComboBox ItemsSource 绑定(bind)到 Enum

c# - WPF 组合框默认值(请选择)

c# - SelectCommand.Connection 属性尚未初始化

c# - ASP.Net Core JWT token 验证

android - 使用 kotlin 在 android 中与底部工作表进行数据绑定(bind)