2016-11-21 35 views
1

我是新來WPF使用兩個代碼的ViewModels在XAML請幫我任何one.i有兩個的ViewModels 1.ItemCategoryViewModel和2.TypeViewmodel這兩種視圖模式來不同的兩個表如何使用TabControl的

<tabcontrol> 
    <TabItem x:Name="itemtype" Header="ItemType" IsSelected="True" MinHeight="10"> 
     <Grid/> 
    </tabitem> 
    <TabItem Header="ItemCategory" Margin="-1,0" MinHeight="10"> 
     <grid/> 
    </tabitem> 
</tabcontrol> 

在xaml.cs代碼中,我想使用兩個viewmodels。

TypeViewmodel povm = new TypeViewmodel(); 

ItemCategoryViewModel tcvm=new ItemCategoryViewModel(); 

public PurchaseOrderEntry() 
{ 
    InitializeComponent(); 
    this.DataContext = povm; 
    this.DataContext = tcvm; 
    txtPONumber.Focus(); 

    if (povm.FocusMoveTo == null) 
     povm.FocusMoveTo = new Action(() => this.FieldNumberToChange()); 

    if (povm.FocusMoveByTabId == null) 
     povm.FocusMoveByTabId = new Action(() =>this.GoToFocusByTabId()); 

    if (povm.OpenDialogue == null) 
     povm.OpenDialogue = new Action<string>(this.OpenDialogue); 

    if (tcvm.FocusMoveTo == null) 
     tcvm.FocusMoveTo = new Action(() => this.FieldNumberToChange()); 

    if (tcvm.FocusMoveByTabId == null) 
     tcvm.FocusMoveByTabId = new Action(() =>this.GoToFocusByTabId()); 

    if (tcvm.OpenDialogue == null) 
     tcvm.OpenDialogue = new Action<string>(this.OpenDialogue); 
} 

當我只執行一個標籤項執行另一個不working.viewmodel重寫。我如何解決請幫助我

回答

0

如何將你的兩個viewmodels包裝在「MainViewModel」中並調整xaml代碼中的綁定?

據我知道你可以在不同的控制一個視圖中使用不同的ViewModels,但根據所選擇的選項卡上我不會改變完整的視圖模型的一個TabControl:

編輯:

public partial class MainWindow : Window 
{ 
    public MainViewModel MainViewModel { get; set; } 
    public MainWindow() 
    { 
     MainViewModel = new MainViewModel(new TypeViewmodel(), new ItemCategoryViewModel()); 
     this.DataContext = MainViewModel; 
     InitializeComponent(); 

    } 
} 
public class MainViewModel 
{ 
    public TypeViewmodel TypeViewmodel { get; set; } 
    public ItemCategoryViewModel ItemCategoryViewmodel { get; set; } 

    public MainViewModel(TypeViewmodel povm, ItemCategoryViewModel tcvm) 
    { 
     this.TypeViewmodel = povm; 
     this.ItemCategoryViewmodel = tcvm; 
    } 
} 

public class ItemCategoryViewModel 
{ 
    public ItemCategoryViewModel() 
    { 
     TextBoxMessageItemCategoryViewModel = "Test - ItemCategoryViewModel"; 
    } 
    public String TextBoxMessageItemCategoryViewModel { get; set; } 
} 

public class TypeViewmodel 
{ 
    public TypeViewmodel() 
    { 
     TextBoxMessageTypeViewmodel = "Test - TypeViewmodel"; 
    } 
    public String TextBoxMessageTypeViewmodel { get; set; } 
} 

的XAML:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <TabControl> 
     <TabItem x:Name="itemtype" Header="ItemType" IsSelected="True" MinHeight="10"> 
      <Grid DataContext="{Binding TypeViewmodel}"> 
       <Label Content="{Binding TextBoxMessageTypeViewmodel}"></Label> 
      </Grid> 
     </TabItem> 
     <TabItem Header="ItemCategory" Margin="-1,0" MinHeight="10" > 
      <Grid DataContext="{Binding ItemCategoryViewmodel}"> 
       <Label Content="{Binding TextBoxMessageItemCategoryViewModel}"></Label> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 

說明: 我創建了兩個真正的小視圖模型,所以你明白了什麼是beeing綁定。 這兩個內部viewmodels都有一個字符串屬性,它將被綁定在你的標籤中。 你可以用任何你想綁定的東西來替換它。

在Xaml中,您可以看到TabControl中的每個網格綁定到MainViewModel的屬性,該屬性已經是整個View的DataContext(在我的示例窗口中)。

<Grid DataContext="{Binding TypeViewmodel}"> 

所以在這一點上,你(你可以在TabControl的本身藏漢結合)的DataContext是TypeViewmodel(爲網格內所有的元素!),所以你可以綁定拉布勒內容到TypeViewModel的財產類:

<Label Content="{Binding TextBoxMessageTypeViewmodel}"></Label> 

嗯,這是真的基本的約束力 - 你應該在任何教程藏漢找到這個......

希望這有助於你。

+0

太感謝你了,但我怎麼寫XmaI和xaml.cs,有沒有在XAML –

+0

任何改變,我會編輯我的回答你,讓它變得更清晰,好嗎? – TripleEEE

+0

這是否幫助你,還是你需要更詳細的幫助? – TripleEEE

0

與此代碼替換此代碼

this.DataContext = povm; 
    this.DataContext = tcvm; 

this.itemtype.DataContext = povm; 
this.ItemCategory.DataContext = tcvm;