I am relatively new to WPF and sometimes it makes my head explode. However, I do like the power behind it, especially when used with the MVVM model.
I have a ControlTemplate
that contains a Button
. I use that ControlTemplate
inside of a custom control. I want to add a property on the custom control that will bind to the command property of the Button
inside the ControlTemplate
. Basically, it is a ComboBox
with a Button
to the right of it to allow a user to pop up a search dialog. Since this control could appear on a usercontrol multiple times, I need to be able to potentially bind each control to a different command (search products, search customers, etc).
However, I have been unable to figure out how to do this.
Here is some sample XAML:
<Style TargetType="{x:Type m:SelectionFieldControl}">
<Setter Property="LookupTemplate" Value="{StaticResource LookupTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type m:SelectionFieldControl}">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
SnapsToDevicePixels="True"
Focusable="False">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="0"
SharedSizeGroup="{Binding LabelShareSizeGroupName,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:BaseFieldControl}}}" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="{Binding WidgetsShareSizeGroupName,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:BaseFieldControl}}}" />
</Grid.ColumnDefinitions>
<!-- Customized Value Part -->
<ComboBox x:Name="PART_Value"
Grid.Column="1"
Margin="4,2,0,1"
SelectedValue="{Binding Path=SelectionField.Value,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:SelectionFieldControl}}}"
IsEnabled="{Binding Field.IsNotReadOnly,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type m:SelectionFieldControl}}}"
Visibility="{Binding Field.IsInEditMode, Converter={StaticResource TrueToVisible},
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type m:SelectionFieldControl}}}"
FontFamily="{StaticResource FontFamily_Default}" FontSize="11px">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsVirtualizing="True"
VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<StackPanel Grid.Column="2"
Orientation="Horizontal"
Name="PART_Extra"
Focusable="False">
<ContentControl Name="PART_LookupContent"
Template="{Binding LookupTemplate,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:SelectionFieldControl}}}"
Focusable="False"/>
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I thought I could get it to work by doing something like this:
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type SelectionFieldControl}}, Path=ShowSearchCommand}" Margin="2" />
but it does not work.
Any help would be greatly appreciated.