It's hard to explain, but I will do my best. I wanted to have a reusable control that had 3 buttons, one for creating entities, another for publishing and another for deleting, here abbreviated XAML of the corresponding part.
-
<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 dpprops declarations:
public uscActionButtons()
{
InitializeComponent();
this.DataContext = this;
}
public ICommand NewCommand
{
get { return (ICommand)GetValue(NewCommandProperty); }
set { SetValue(NewCommandProperty, value); }
}
public static readonly DependencyProperty NewCommandProperty =
DependencyProperty.Register("NewCommand", typeof(ICommand), typeof(uscActionButtons), new UIPropertyMetadata(null, new PropertyChangedCallback(OnCommandChanged)));
I wanted to associate the NewCommand property with a specific implementation of it in another control. Usage example:
<common:uscActionButtons Grid.Row="0" HorizontalAlignment="Left"
NewCommand="{Binding NewItemCommand}"
/>
and
public ICommand NewItemCommand
{
get
{
if (mNewItemCommand == null)
{
mNewItemCommand = new RelayCommand(x => this.CreateItem());
}
return mNewItemGroupCommand;
}
}
, (ActionButtons) NewItemCommand.
, . , "" .
, , WPF Command, , , ICommand.
?
edit: , , , RelativeSource 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>