私はobservableコレクションにバインドされたtabControlを持っています。 headerTemplateでは、文字列プロパティにバインドしたいと思いますが、contentTemplateではユーザーコントロールを配置しました。tabControlの複数のuserControlインスタンス
ここMainWindow.xamlのためのコードです:
<Grid>
<Grid.Resources>
<DataTemplate x:Key="contentTemplate">
<local:UserControl1 />
</DataTemplate>
<DataTemplate x:Key="itemTemplate">
<Label Content="{Binding Path=Name}" />
</DataTemplate>
</Grid.Resources>
<TabControl IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Pages}"
ItemTemplate="{StaticResource itemTemplate}"
ContentTemplate="{StaticResource contentTemplate}"/>
</Grid>
そして背後にそのコード:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
public class MainWindowViewModel
{
public ObservableCollection<PageViewModel> Pages { get; set; }
public MainWindowViewModel()
{
this.Pages = new ObservableCollection<PageViewModel>();
this.Pages.Add(new PageViewModel("first"));
this.Pages.Add(new PageViewModel("second"));
}
}
public class PageViewModel
{
public string Name { get; set; }
public PageViewModel(string name)
{
this.Name = name;
}
}
だから、このシナリオでの問題は、(ItemTemplateに同様のControlTemplateを指定した)であります私はという1つのというユーザーコントロールのインスタンスしか取得できません。そこには、バインドされている各アイテムのインスタンスがあります。
_「各アイテムのインスタンスが必要です」_ - ここで必要性を明確にしてください。 _Why_あなたは具体的に各項目のために新しいインスタンスを1つ持っていますか?具体的に言えば、問題を確実に再現する良い[mcve]を提供し、コードが何をしているのか、それがあなたがしたいこととどのように違うのかを正確に説明してください。 _(遅い時間にチンミングすることについて私の謝罪...私はこの質問(別のものに答える)(http://stackoverflow.com/q/33125188)を横断しただけで、これは私が他の1つで、私は明確化を求めるだろうと思った)_。 –