切换标签后WPF组合框选择变化 [英] WPF ComboBox selection change after switching tabs

查看:141
本文介绍了切换标签后WPF组合框选择变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了基于嵌套标签的项目。
中的嵌套标签是相同的viemModel和相同的用户界面的不同的实例。 $ B,当我在标签之间切换$ B他组合框出现在标签chenge兵卫的选择取决于所失去焦点的选项卡上。



我同时添加的ViewModels和鉴于我的测试项目。
感谢您在您的帮助。



主窗口

 < Window.Resources> 

<数据类型的DataTemplate ={X:类型本地:IntermediateViewModel}>
<局部:IntermediateView />
< / DataTemplate中>

<的DataTemplate X:键=HeaderedTabItemTemplate>
<网格和GT;
< ContentPresenter
含量={绑定路径=报头,UpdateSourceTrigger =的PropertyChanged}
VerticalAlignment =中心>
< / ContentPresenter>
< /网格和GT;
< / DataTemplate中>

<风格X:键=SimpleTabItemStyle的TargetType =TabItem的>
< setter属性=前景VALUE =白/>
< setter属性=粗细VALUE =大胆/>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型TabItem的}>
<网格和GT;
< BORDER NAME =边框了borderThickness =1BorderBrush =#555959>
< ContentPresenter X:NAME =ContentSiteVerticalAlignment =中心的Horizo​​ntalAlignment =中心
ContentSource =头保证金=12,2,12,2RecognizesAccessKey =真身高=40了minWidth =90/>
< /边框>
< /网格和GT;
< ControlTemplate.Triggers>
<触发属性=IsSelectedVALUE =真>
<二传手的TargetName =边框属性=背景VALUE =#555959/>
< /触发>
< /ControlTemplate.Triggers>
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;

<的DataTemplate X:键=DefaultTabControlTemplate>
< TabControl的IsSynchronizedWithCurrentItem =真
了borderThickness =0
的ItemsSource ={结合}
的ItemTemplate ={StaticResource的HeaderedTabItemTemplate}
ItemContainerStyle = {StaticResource的SimpleTabItemStyle}
的SelectionChanged =TabControl_SelectionChanged
/>
< / DataTemplate中>


<!---->


< /Window.Resources>

<电网了minHeight =200了minWidth =300>
< Grid.RowDefinitions>
< RowDefinition HEIGHT =260 */>
< RowDefinition HEIGHT =51 */>
< /Grid.RowDefinitions>
< BORDER>
< ContentControl中
含量={绑定路径=工作区}
的ContentTemplate ={DynamicResource DefaultTabControlTemplate}
/>
< /边框>
<按钮Grid.Row =1CONTENT =add命令={结合AddCommand}/>
< /网格和GT;



视图模型(创建一个不同的istance每次)

 类MainWindowViewModel:WorkspacesViewModel< IntermediateViewModel> 
{
公共MainWindowViewModel()
{
this.WorkspacesView.CurrentChanged + =新的EventHandler(WorkspacesView_CurrentChanged);
}

无效WorkspacesView_CurrentChanged(对象发件人,EventArgs五)
{
}

RelayCommand myVar的= NULL;
公众的ICommand AddCommand
{
得到
{
返回myVar的? (myVar的=新RelayCommand(参数=>
{
SetWindow(新IntermediateViewModel(AA+ this.Workspaces.Count));
}));
}
}



第一级标签



 < UserControl.Resources> 

<数据类型的DataTemplate ={X:类型本地:ClassViewModel}>
<局部:ClassView中/>
< / DataTemplate中>
< /UserControl.Resources>

< BORDER>
< ContentControl中CONTENT ={绑定路径= CurrentWorkspace,模式=单向}加载=ContentControl_LoadedDataContextChanged仅=ContentControl_DataContextChangedIsVisibleChanged在=ContentControl_IsVisibleChangedLayoutUpdated =ContentControl_LayoutUpdatedTargetUpdated =ContentControl_TargetUpdated卸载=ContentControl_Unloaded />
< /边框>





第一级视图模型



类IntermediateViewModel:WorkspacesViewModel
{
公共字符串头{搞定;组; }

 公共IntermediateViewModel(串标头)
{
=头头;
SetWindow(新ClassViewModel(头));
}
}



嵌套的标签



 < UserControl.Resources> 
< CollectionViewSource X:键=StatusView来源={绑定路径= StatusList}/>
< /UserControl.Resources>
<网格和GT;
<组合框名称=_ spl2Status的ItemsSource ={绑定源= {StaticResource的StatusView}}
的SelectedValue ={绑定路径= myProperty的,模式=双向,UpdateSourceTrigger =的PropertyChanged}
SelectedValuePath =FL_TYPE
的DisplayMemberPath =ID_TYPE保证金=76,12,0,0HEIGHT =40VerticalAlignment =顶部的Horizo​​ntalAlignment =左WIDTH =146
DataContextChanged仅=_ spl2Status_DataContextChanged
IsVisibleChanged在=_ spl2Status_IsVisibleChanged
加载=_ spl2Status_Loaded
的SelectionChanged =_ spl2Status_SelectionChanged

< /组合框>
< /网格和GT;



嵌套的标签浏览模式



 公共枚举myTypes 
{
tipo0 = 0,
tipo1 = 1,
tipo2 = 2,
}

类ClassViewModel:WorkspaceViewModel
{
公共ClassViewModel(字符串名称)
{
名称=名称;
}

公共字符串名称{;组; }

私人列表< IntEnumType> _statusList = NULL;
公开名单< IntEnumType> StatusList
{
得到
{
如果(_statusList == NULL)
_statusList =新的List< IntEnumType>()
{
新IntEnumType((int)的myTypes.tipo0,myTypes.tipo0.ToString()),
新IntEnumType((int)的myTypes.tipo1,myTypes.tipo1.ToString()),
新IntEnumType((中间体)myTypes.tipo2,myTypes.tipo2.ToString()),
};
返回_statusList;
}
}

私人INT myVar的= 1;
公众诠释myProperty的
{
得到
{
返回myVar的;
}

{
如果(myVar的=价值!)
{
myVar的=价值;
OnPropertyChanged(()=> myProperty的);
}
}
}
}

公共类TabItemStyleSelector:StyleSelector
{
的大众风格MainTabItem {搞定;组; }
的大众风格ChildrenTa​​bItem {搞定;组; }
的大众风格SpecificationTabItem {搞定;组; }

公众覆盖风格SelectStyle(对象项目,DependencyObject的容器)
{
//如果(产品IHOME)
//返回MainTabItem;
//否则,如果(产品SpecificationItemViewModel)
//返回SpecificationTabItem;
//别的
返回ChildrenTa​​bItem;
}
}


解决方案

的代码是有点难以完全跟随,但我猜测,这个问题是只有一个实例的 ClassViewModel ,这就是选择组合框存储 {绑定路径= myProperty的,所以无论是存储在 myProperty的将在组合框中的所有实例中反映出来无论他们生活在哪里。


I made a project based on nested tabs. the nested tabs are different instance of the same viemModel and the same UI. when I switch between the tabs he comboboxes present in the tabs chenge thei selection depending on the tab that is loosing focus.

I add both the viewmodels and the view of my test project. thank you in advance for your help

main window

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

view model (create a different istance each time)

class MainWindowViewModel : WorkspacesViewModel<IntermediateViewModel>
{
    public MainWindowViewModel()
    {
        this.WorkspacesView.CurrentChanged += new EventHandler(WorkspacesView_CurrentChanged);
    }

    void WorkspacesView_CurrentChanged(object sender, EventArgs e)
    {
    }

    RelayCommand myVar = null;
    public ICommand AddCommand
    {
        get 
        {
            return myVar ?? (myVar = new RelayCommand(param => 
            {
                SetWindow(new IntermediateViewModel("AA" + this.Workspaces.Count) );
            })); 
        }
    }

first level tab

    <UserControl.Resources>

    <DataTemplate DataType="{x:Type local:ClassViewModel}">
        <local:ClassView />
    </DataTemplate>
</UserControl.Resources>

<Border>
    <ContentControl Content="{Binding Path=CurrentWorkspace, Mode=OneWay}" Loaded="ContentControl_Loaded" DataContextChanged="ContentControl_DataContextChanged" IsVisibleChanged="ContentControl_IsVisibleChanged" LayoutUpdated="ContentControl_LayoutUpdated" TargetUpdated="ContentControl_TargetUpdated" Unloaded="ContentControl_Unloaded" />
</Border>

first level viewmodel

class IntermediateViewModel : WorkspacesViewModel { public string Header { get; set; }

    public IntermediateViewModel(string header)
    {
        Header = header;
        SetWindow(new ClassViewModel(header));
    }
}

nested tab

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

nested tab view model

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

解决方案

The code is a little hard to completely follow, but I'm guessing that the issue is that there is only one instance of your ClassViewModel and it is where the selection for the combo box is stored {Binding Path=MyProperty, so whatever is stored in MyProperty will be reflected in all instances of the combo box regardless of where they live.

这篇关于切换标签后WPF组合框选择变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆