3

私はWPFに比較的慣れていないため、頭が爆発することがあります。ただし、特に MVVM モデルで使用する場合は、その背後にある力が気に入っています。

ControlTemplate含む がありButtonます。ControlTemplateカスタム コントロール内で使用します。Button内のコマンド プロパティにバインドするカスタム コントロールにプロパティを追加したいと考えていControlTemplateます。基本的に、ユーザーが検索ダイアログをポップアップできるようにするために、その右側に がありますComboBoxButtonこのコントロールはユーザーコントロールに複数回表示される可能性があるため、各コントロールを異なるコマンド (製品の検索、顧客の検索など) に潜在的にバインドできるようにする必要があります。

しかし、私はこれを行う方法を理解できませんでした。

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>

私は次のようなことをすることでそれを機能させることができると思いました:

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

しかし、それは機能しません。

どんな助けでも大歓迎です。

4

1 に答える 1

1

どっ!

それはうまくいきます。問題は、プロパティを登録するときに、依存関係プロパティがプロパティ メタ データに 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)
    {

    }

于 2010-05-20T19:46:33.153 に答える