C# 切换选项卡后更改WPF组合框选择

C# 切换选项卡后更改WPF组合框选择,c#,wpf,mvvm,combobox,tabs,C#,Wpf,Mvvm,Combobox,Tabs,我制作了一个基于嵌套选项卡的项目。 嵌套选项卡是相同viemModel和相同UI的不同实例。 当我在选项卡之间切换时,选项卡中的组合框会根据失去焦点的选项卡显示选择 我添加了viewmodels和测试项目的视图。 提前感谢您的帮助 主窗口 <Window.Resources> <DataTemplate DataType="{x:Type local:IntermediateViewModel}"> <local:IntermediateV

我制作了一个基于嵌套选项卡的项目。 嵌套选项卡是相同viemModel和相同UI的不同实例。 当我在选项卡之间切换时,选项卡中的组合框会根据失去焦点的选项卡显示选择

我添加了viewmodels和测试项目的视图。 提前感谢您的帮助

主窗口

<Window.Resources>

    <DataTemplate DataType="{x:Type local:IntermediateViewModel}">
        <local:IntermediateView />
    </DataTemplate>

    <DataTemplate x:Key="HeaderedTabItemTemplate">
        <Grid>
            <ContentPresenter
                        Content="{Binding Path=Header, UpdateSourceTrigger=PropertyChanged}" 
                        VerticalAlignment="Center" >
            </ContentPresenter>
        </Grid>
    </DataTemplate>

    <Style x:Key="SimpleTabItemStyle" TargetType="TabItem">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Grid>
                        <Border Name="Border" BorderThickness="1" BorderBrush="#555959">
                            <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center"
                                 ContentSource="Header" Margin="12,2,12,2" RecognizesAccessKey="True" Height ="40" MinWidth ="90"/>
                        </Border>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="#555959" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <DataTemplate x:Key="DefaultTabControlTemplate">
        <TabControl IsSynchronizedWithCurrentItem="True" 
                        BorderThickness="0" 
                        ItemsSource="{Binding}" 
                        ItemTemplate="{StaticResource HeaderedTabItemTemplate}"
                        ItemContainerStyle="{StaticResource SimpleTabItemStyle}"
                        SelectionChanged="TabControl_SelectionChanged"
                        />
    </DataTemplate>


    <!---->


</Window.Resources>

<Grid MinHeight="200" MinWidth="300">
    <Grid.RowDefinitions>
        <RowDefinition Height="260*" />
        <RowDefinition Height="51*" />
    </Grid.RowDefinitions>
    <Border >
        <ContentControl 
            Content="{Binding Path=Workspaces}" 
            ContentTemplate="{DynamicResource DefaultTabControlTemplate}"
             />
    </Border>
    <Button Grid.Row="1" Content="Add" Command="{Binding AddCommand}"/>
</Grid>
嵌套选项卡

    <UserControl.Resources>
    <CollectionViewSource x:Key="StatusView" Source="{Binding Path=StatusList}"/>
</UserControl.Resources>
<Grid>
    <ComboBox Name="_spl2Status" ItemsSource="{Binding Source={StaticResource StatusView}}"
      SelectedValue="{Binding Path=MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      SelectedValuePath="FL_TYPE"
      DisplayMemberPath="ID_TYPE" Margin="76,12,0,0" Height="40" VerticalAlignment="Top" HorizontalAlignment="Left" Width="146"
               DataContextChanged="_spl2Status_DataContextChanged"
               IsVisibleChanged="_spl2Status_IsVisibleChanged"
               Loaded="_spl2Status_Loaded"
                SelectionChanged="_spl2Status_SelectionChanged"
               >
    </ComboBox>
</Grid>

嵌套选项卡视图模型

public enum myTypes
{ 
    tipo0 = 0,
    tipo1 = 1,
    tipo2 = 2,
}

class ClassViewModel : WorkspaceViewModel
{
    public ClassViewModel(string name)
    {
        Name = name;
    }

    public string Name { get; set; }

    private List<IntEnumType> _statusList = null;
    public List<IntEnumType> StatusList
    {
        get
        {
            if (_statusList == null)
                _statusList = new List<IntEnumType>()
                {
                    new IntEnumType((int)myTypes.tipo0, myTypes.tipo0.ToString()),
                    new IntEnumType((int)myTypes.tipo1, myTypes.tipo1.ToString()),
                    new IntEnumType((int)myTypes.tipo2, myTypes.tipo2.ToString()),
                };
            return _statusList;
        }
    }

    private int myVar = 1;
    public int MyProperty
    {
        get 
        {
            return myVar; 
        }
        set 
        {
            if (myVar != value)
            {
                myVar = value;
                OnPropertyChanged(() => MyProperty);
            }
        }
    }
}

public class TabItemStyleSelector : StyleSelector
{
    public Style MainTabItem { get; set; }
    public Style ChildrenTabItem { get; set; }
    public Style SpecificationTabItem { get; set; }

    public override Style SelectStyle(object item, DependencyObject container)
    {
        //if (item is IHome)
        //    return MainTabItem;
        //else if (item is SpecificationItemViewModel)
        //    return SpecificationTabItem;
        //else
            return ChildrenTabItem;
    }
}
公共枚举MyType
{ 
tipo0=0,
tipo1=1,
tipo2=2,
}
类ClassViewModel:WorkspaceViewModel
{
公共类视图模型(字符串名称)
{
名称=名称;
}
公共字符串名称{get;set;}
私有列表_statusList=null;
公共列表状态列表
{
得到
{
如果(_statusList==null)
_statusList=新列表()
{
新的IntentumType((int)myTypes.tipo0,myTypes.tipo0.ToString()),
新的IntentumType((int)myTypes.tipo1,myTypes.tipo1.ToString()),
新的IntentumType((int)myTypes.tipo2,myTypes.tipo2.ToString()),
};
返回状态列表;
}
}
私有int-myVar=1;
公共财产
{
得到
{
返回myVar;
}
设置
{
if(myVar!=值)
{
myVar=价值;
OnPropertyChanged(()=>MyProperty);
}
}
}
}
公共类选项卡ItemStyleSelector:StyleSelector
{
公共样式MainTabItem{get;set;}
公共样式ChildrenTabItem{get;set;}
公共样式规范tabitem{get;set;}
公共替代样式SelectStyle(对象项,DependencyObject容器)
{
//如果(项目为IHome)
//返回MainTabItem;
//else if(项目为SpecificationItemViewModel)
//退货规格表项;
//否则
返回ChildrenTabItem;
}
}

代码有点难以完全理解,但我猜问题在于您的
ClassViewModel
只有一个实例,它是组合框选择的存储位置
{Binding Path=MyProperty
,因此存储在
MyProperty
中的内容将反映在组合框的所有实例中,无论它们位于何处。

问题在于加载的事件处理程序中

切换选项卡时,卸载一个选项卡并加载一个新选项卡


我怀疑您正在更改MyComboBox.SelectedIndex(在
\u spl2Status\u加载的
中)。这有点晚了,但由于我面临同样的问题,我想与大家分享我的分析

更改选项卡时,将当前选项卡的DataContext更改为其他ViewModel,从而也更改组合框的ItemsSource

如果您以前选择的项目(SelectedItem)不包含在新项目资源中,组合框将触发SelectionChanged事件,因此将SelectedIndex设置为-1

尽管ComboBox的这种默认行为可能有道理,但在许多情况下,它非常烦人


我们从ComboBox派生了一个自己的类来处理这个问题。但是这不是很令人满意,因为您可能会失去一些最需要的默认行为。

不,有两个viewmodels。如果一个有两个不同值的文本框,那么问题就出在ComboBox上,然后请发布第二个viewmodel。两者都绑定到相同的SelectedValue或者SelectedIndex将解释此行为。还添加了其他ViewModels如果保持绑定不变,只需将combobox更改为文本框,则每个文本框都保留其原始值?完全正确…错误仅限于combo..datagrid加载两个不同的集合可以正常工作,但comboboxes@Pille-你的回答我It’理解这个问题很有帮助。有什么解决办法吗?
    public IntermediateViewModel(string header)
    {
        Header = header;
        SetWindow(new ClassViewModel(header));
    }
}
    <UserControl.Resources>
    <CollectionViewSource x:Key="StatusView" Source="{Binding Path=StatusList}"/>
</UserControl.Resources>
<Grid>
    <ComboBox Name="_spl2Status" ItemsSource="{Binding Source={StaticResource StatusView}}"
      SelectedValue="{Binding Path=MyProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      SelectedValuePath="FL_TYPE"
      DisplayMemberPath="ID_TYPE" Margin="76,12,0,0" Height="40" VerticalAlignment="Top" HorizontalAlignment="Left" Width="146"
               DataContextChanged="_spl2Status_DataContextChanged"
               IsVisibleChanged="_spl2Status_IsVisibleChanged"
               Loaded="_spl2Status_Loaded"
                SelectionChanged="_spl2Status_SelectionChanged"
               >
    </ComboBox>
</Grid>
public enum myTypes
{ 
    tipo0 = 0,
    tipo1 = 1,
    tipo2 = 2,
}

class ClassViewModel : WorkspaceViewModel
{
    public ClassViewModel(string name)
    {
        Name = name;
    }

    public string Name { get; set; }

    private List<IntEnumType> _statusList = null;
    public List<IntEnumType> StatusList
    {
        get
        {
            if (_statusList == null)
                _statusList = new List<IntEnumType>()
                {
                    new IntEnumType((int)myTypes.tipo0, myTypes.tipo0.ToString()),
                    new IntEnumType((int)myTypes.tipo1, myTypes.tipo1.ToString()),
                    new IntEnumType((int)myTypes.tipo2, myTypes.tipo2.ToString()),
                };
            return _statusList;
        }
    }

    private int myVar = 1;
    public int MyProperty
    {
        get 
        {
            return myVar; 
        }
        set 
        {
            if (myVar != value)
            {
                myVar = value;
                OnPropertyChanged(() => MyProperty);
            }
        }
    }
}

public class TabItemStyleSelector : StyleSelector
{
    public Style MainTabItem { get; set; }
    public Style ChildrenTabItem { get; set; }
    public Style SpecificationTabItem { get; set; }

    public override Style SelectStyle(object item, DependencyObject container)
    {
        //if (item is IHome)
        //    return MainTabItem;
        //else if (item is SpecificationItemViewModel)
        //    return SpecificationTabItem;
        //else
            return ChildrenTabItem;
    }
}