2012-01-19 2 views
0

MVVMパターンを使用してタブコントロールにタブ項目を追加する非常に簡単で基本的なアプリケーションを作成しようとしています。 だから私は、作成: ボタン一つで簡単なビュー - 「CustomerView.xaml」TabControlにタブコレクションが表示されない

空のViewModelクラス - それは空の原因は、ビューが保存またはViewmodalから任意の情報を抽出していないです(一つだけのボタンがあります) - 「CustomerViewModel.cs」

メイン・ウィンドウクラスコードがCustomerViewModel の観察可能なコレクションを保持し、一つの「顧客の追加」ボタンを持っている - タブコントロールとタブコントロール自体に顧客のタブ項目を追加します。

私はコマンドを使用しないため、現時点では関係がありません。コレクションに新しいCustomerViewModelを追加すると表示される新しいタブアイテムでした。

結果は、ObservableコレクションにCustomerViewModelsが追加されているのがわかりますが、tabcontrolにtabitemsが追加されていないことがわかります。コレクションはtabcontrolを更新していません。

これはメインウィンドウXAMLである:

<Window x:Class="MyViewModalTabControl.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

    xmlns:vm="clr-namespace:MyViewModalTabControl.ViewModal" 
    xmlns:vw="clr-namespace:MyViewModalTabControl.Views" 

    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 

    <DataTemplate DataType="{x:Type vm:CustomerViewModel}"> 
     <vw:CustTabView /> 
    </DataTemplate> 

    <DataTemplate x:Key="ClosableTabItemTemplate"> 
     <DockPanel Width="120"> 
      <Button 
      Content="X" 
      Cursor="Hand" 
      DockPanel.Dock="Right" 
      Focusable="False" 
      FontFamily="Courier" 
      FontSize="9" 
      FontWeight="Bold" 
      Margin="0,1,0,0" 
      Padding="0" 
      VerticalContentAlignment="Bottom" 
      Width="16" Height="16" 
      /> 
        <ContentPresenter 
      Content="Sample" 
      VerticalAlignment="Center" 
      /> 
     </DockPanel> 
    </DataTemplate> 

</Window.Resources> 

<Grid Margin="4" ShowGridLines="True"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="100"/> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

    <Button Name="CustTabButton" Content="New Customer" Height="30" Margin="12,136,9,136" Click="CustTabButton_Click"></Button> 


    <TabControl Grid.Column="1" Grid.Row="0" Background="Red" 
       ItemsSource="{Binding CustomerTabs}" 
       ItemTemplate="{StaticResource ClosableTabItemTemplate}" 
       > 


    </TabControl> 
</Grid> 

これは、メインウィンドウの背後にあるコードである:

public partial class MainWindow : Window 
{ 
    private ObservableCollection<CustomerViewModel> _customertabs; 
    public ObservableCollection<CustomerViewModel> CustomerTabs 
    { 
     get 
     { 
      if (_customertabs == null) 
      { 
       _customertabs = new ObservableCollection<CustomerViewModel>(); 
       // _workspaces.CollectionChanged += this.OnWorkspacesChanged; 
      } 
      return _customertabs; 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void CustTabButton_Click(object sender, RoutedEventArgs e) 
    { 

     CustomerViewModel CustomerWorkSpace = new CustomerViewModel(); 
     this.CustomerTabs.Add(CustomerWorkSpace); 

    } 


} 

これは、ビューモデルのクラスである:

public class CustomerViewModel : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

} 

これはtです彼は見る:

UserControl x:Class="MyViewModalTabControl.Views.CustTabView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<Grid> 
    <Button Name="CustTabButton" Content="New Customer" Height="30" Margin="12,136,9,136"></Button> 
</Grid> 

は私が何をしないのですか?

答えて

0

修正点:

public MainWindow() 
{ 
    InitializeComponent(); 
    this.DataContext=this; 
} 
2

ここで、メインウィンドウのdatacontextを設定しますか?あなたのバインディングは、正しいDatacontextで動作します。

メインビューモデルを作成する方が良いでしょう。これは現時点でmainwindow.csに入れたものを処理しますか?

EDIT:plsをご覧ください。this msdn post from josh smith。そこにも閉じ可能なタブがあります。

+0

私はdatacontextをまったく設定しません。どこ?私は私が親コントロール、または何かのような何かの継承を持っているときdatacontextを使用する必要があると思った....ここで私はちょうど1つのtabcontrol、thatsを持っています。 – Rodniko

+1

データバインディングを設定しないと、このバインディングは動作しません。 blindmeis

+0

私の編集例を参照してください – blindmeis

0

ClosableTabItemTemplateは、タブコントロールに表示される「リターン」TabItemないDockPanel

  • code
  • でそれを行う TabItem制御用 templateを作成する必要があり、次の

    • のいずれかをお試しください
    関連する問題