2013-03-20 122 views
0

我做了一個基於嵌套選項卡的項目。 嵌套選項卡是相同的viemModel和相同的用戶界面的不同實例。 當我在標籤之間切換時,他在組合標籤中顯示的組合框取決於失去焦點的標籤。切換選項卡後WPF組合框選擇更改

我添加了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> 

視圖模型(創建一個不同的istance每次)

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

第一級標籤

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

一級視圖模型

class IntermediateViewModel:WorkspacesViewModel { {0}公共字符串標題{get;組; }

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

回答

1

的代碼是一個有點難以完全效仿,但我猜測,這個問題是有隻有你的ClassViewModel的一個實例,這是存儲組合框的選擇{Binding Path=MyProperty,所以無論存儲在MyProperty將反映在所有我無論他們居住在哪裏,組合框的物質。

+0

不,有2個viewmodels ..如果有一個文本框有兩個不同的值它的工作原理,問題就在於組合框 – andrea 2013-03-20 18:49:24

+0

然後請發佈第二個viewmodel。都綁定到相同的SelectedValue或SelectedIndex將解釋此行爲。 – Paparazzi 2013-03-20 18:53:21

+0

也添加了其他視圖模型 – andrea 2013-03-20 18:54:45

-2

問題出在您的加載事件處理程序中。

當您切換選項卡時,卸載一個選項卡並加載一個新選項卡。我想你的MyComboBox.SelectedIndex_spl2Status_Loaded

相關問題