2010-05-20 2 views
3

저는 WPF에 비교적 익숙하지 않습니다. 때로는 머리가 터질 수도 있습니다. 그러나 MVVM 모델과 함께 사용할 경우 특히 그 배후에있는 기능을 좋아합니다.WPF에서 컨트롤 템플릿의 명령을 부모의 속성에 바인딩하려면 어떻게해야합니까?

나는 Button을 포함하는 ControlTemplate입니다. 나는 사용자 정의 컨트롤 내부에 ControlTemplate을 사용합니다. ControlTemplate 안에있는 Button의 명령 속성에 바인딩 할 사용자 지정 컨트롤에 속성을 추가하고 싶습니다. 기본적으로 사용자 오른쪽에 Button이있는 ComboBox이며 사용자가 검색 대화 상자를 팝업 할 수 있습니다. 이 컨트롤은 여러 번 사용자 컨트롤에 나타날 수 있으므로 잠재적으로 각 컨트롤을 다른 명령 (검색 제품, 검색 고객 등)에 바인딩 할 수 있어야합니다.

그러나이 작업을 수행하는 방법을 파악하지 못했습니다.

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

나는 그것을 이런 식으로 뭔가를 수행하여 작동시킬 수 있다고 생각 : 여기

는 일부 샘플 XAML입니다

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

그러나 그것은 작동하지 않습니다.

도움을 주시면 감사하겠습니다.

답변

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

    }