public class ToolBarView : ToolBar { public ToolBarView() { this.DataContext = new ToolBarViewModel(); } } public ToolBarViewModel: ViewModelBase { public ObservableCollection<ViewModelBase> Items {get;set;} public ToolBarViewModel() { // populate button view models Items.Add(new ButtonViewModel() {Content="Button1"}); Items.Add(new ButtonViewModel() {Content="Button2"}); } } public class ButtonView : Button { public ButtonView() { this.DataContext = new ButtonViewModel(); } } public class ButtonViewModel : ViewModelBase { public object Content {get;set;} } In MainWindow.xaml
<Window.Resources> <DataTemplate x:Key="buttonTemplate" DataType="{x:Type vm:ButtonViewModel}"> <v:ButtonView Content={Binding Content}/> </DataTemplate> <v:ToolBarView ItemsSource="{Binding Items}" ItemTemplate={StaticResource buttonTemplate}/> Note: I did INotifyChanged in ViewModelBase class
In MainWindow.xaml. i think My template is wrong.ButtonView in DataTemplate is creating a new view instance. It is not binding the viewModel that was poplulated in the ToolBar Items collection. I tried to do with Relative Binding. Still not successful. Please help me out.