It's hard to explain but i'll do my best. I wanted to have a reusable control that had 3 buttons, one meant for creation of entities, another for edition and another for deletion, here's the abbreviated XAML of the relevant part.
--
<!-- ActionButtons.xaml --> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top"> <Button Name="btnNew" Content="New" Command="{Binding Path=NewCommand}" /> <Button Name="btnEdit" Content="Edit" Command="{Binding Path=EditCommand, Mode=OneWay}" /> <Button Name="btnDelete" Content="Delete" Command="{Binding Path=DeleteCommand, Mode=OneWay}" /> </StackPanel> --
Then, in the code behind I have the dpprops declarations:
// ActionButtons.xaml.cs public uscActionButtons() { InitializeComponent(); this.DataContext = this; } public ICommand NewCommand { get { return (ICommand)GetValue(NewCommandProperty); } set { SetValue(NewCommandProperty, value); } } // Using a DependencyProperty as the backing store for NewCommand. This enables animation, styling, binding, etc... public static readonly DependencyProperty NewCommandProperty = DependencyProperty.Register("NewCommand", typeof(ICommand), typeof(uscActionButtons), new UIPropertyMetadata(null, new PropertyChangedCallback(OnCommandChanged))); I wanted to bind the NewCommand property to a specific implementation of it in another control. Sample intended usage:
<!-- SomeControl.xaml --> <common:uscActionButtons Grid.Row="0" HorizontalAlignment="Left" NewCommand="{Binding NewItemCommand}" /> And
// SomeControlViewModel.cs // Note: SomeControlViewModel IS in the DataContext of SomeControl. public ICommand NewItemCommand { get { if (mNewItemCommand == null) { mNewItemCommand = new RelayCommand(x => this.CreateItem()); } return mNewItemGroupCommand; } } The problem is that the reusable control (ActionButtons) is not seeing the NewItemCommand. If I use a simple button, it sees it fine. It seems the problem is this "chained" binding. But I know it's possible, a WPF button has a Command dependency property to which you bind your commands, so it must not be that hard to create my own reusable control that exposes a ICommand dependency property.
Any ideas?
Thank you
edit: here's the solution, all I had to do was use RelativeSource with FindAncestor.
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top"> <Button Name="btnNew" Content="New" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:uscActionButtons}, Path=NewCommand, Mode=OneWay}" /> <Button Name="btnEdit" Content="Edit" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:uscActionButtons}, Path=EditCommand, Mode=OneWay}" /> <Button Name="btnDelete" Content="Delete" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:uscActionButtons}, Path=DeleteCommand, Mode=OneWay}" /> </StackPanel>