WPF组合框在表项上的选择改变

本文关键字:选择 改变 组合 WPF | 更新日期: 2023-09-27 18:04:16

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

它可以完美地与每个WPF控件,但与组合框我有一个奇怪的行为:当焦点选项卡失去焦点时,它会获得应用程序正在关注的选项卡的combox框中的选定项。

如果我从两个不同类型的选项卡切换,一切都能正常工作,你知道吗?谢谢。

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);
    }
}

my制表符是这样处理的:

<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>

和工作区(我的视图模型列表)(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;
        }
    }

WPF组合框在表项上的选择改变

您的问题已经重现。但是我找不到任何问题。请看看下面的代码,你可能会得到解决方案。这是我的解决方案。

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>

您绝对确定正在创建视图模型的新实例吗?如果不是,那么组合框将共享相同的collectionviewsource,这意味着一个组合框中的更改将反映在所有组合框中。我自己也有同样的问题。

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

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

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

ItemsSource="{Binding StatusListViewSource.View}"

我是从vb转换过来的,所以它可能需要一些编辑。这有帮助吗?