In WPF, how do I get a command in a control template to bind to a property in the parent?

I'm relatively new to WPF, and sometimes it makes my head explode. However, I like the power behind it, especially when used with the MVVM model.

I have ControlTemplateone that contains Button. I use ControlTemplateinside a custom control. I want to add a property to a user control that will be bound to the property of the command Buttoninside ControlTemplate. Basically, this is ComboBoxon the Buttonright of it so that the user can open the search dialog. Since this control can appear several times in usercontrol, I need to be able to potentially associate each control with another command (search for products, search for customers, etc.).

However, I could not figure out how to do this.

Here is an XAML example:

<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>

code>

I thought I could make it work by doing something like this:

<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type SelectionFieldControl}}, Path=ShowSearchCommand}" Margin="2" />

but that will not work.

Any help would be greatly appreciated.

+3
1

Doh!

. , UIPropertyMetadata .

public ICommand ShowSearchCommand { get { return (ICommand)GetValue(ShowSearchCommandProperty); } set { SetValue(ShowSearchCommandProperty, value); } }

    public static readonly DependencyProperty ShowSearchCommandProperty =
        DependencyProperty.Register("ShowSearchCommand", typeof(ICommand),
            typeof(SelectionFieldControl),
            new UIPropertyMetadata(OnShowSearchCommandChanged));

    static void OnShowSearchCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {

    }

>

+1

Source: https://habr.com/ru/post/1746445/


All Articles