In WPF, how do I get a command in a Control Template to bind to a property in a parent?
Posted
by Keith
on Stack Overflow
See other posts from Stack Overflow
or by Keith
Published on 2010-05-20T18:12:28Z
Indexed on
2010/05/20
19:50 UTC
Read the original article
Hit count: 840
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 control template that contains a button. I use that control template 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 control template. Basically, it is a combo box 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 (Searh 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.
© Stack Overflow or respective owner