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>
は私が何をしないのですか?
私はdatacontextをまったく設定しません。どこ?私は私が親コントロール、または何かのような何かの継承を持っているときdatacontextを使用する必要があると思った....ここで私はちょうど1つのtabcontrol、thatsを持っています。 – Rodniko
データバインディングを設定しないと、このバインディングは動作しません。
blindmeis
私の編集例を参照してください – blindmeis