2016-07-21 1 views
1

ControlTemplate의 단추 명령을 CustomControl의 Execute() 메서드에 바인딩합니다. 나는 RoutedCommand를 사용하고 있는데, CanExecute()는 실행되지만 Execute()는 절대 실행하지 않습니다. CustomControl을 주 창에 배치하면 코드가 예상대로 작동합니다. 그것이 Usercontrol에 배치되면, 나는이 문제가 있습니다. 단추 명령 (RelayCommand 등)을 연결하는 몇 가지 방법을 시도했지만 잘못 된 것 같지 않습니다. 어떤 도움을 주셔서 감사합니다.CustomControl에서 명령 바인딩 문제가 발생합니다. CanExecute()가 실행되지 않습니다.

컨텍스트에서는 Xceed 오픈 소스 버전의 초기 포크 인 TokenizingTextBox 컨트롤입니다. 버튼은 토큰 목록에서 토큰을 삭제하기위한 것입니다.

(이자의 버튼을 포함)을 TokenIten의 전체 스타일 :

<Style TargetType="{x:Type local:TokenItem}"> 
     <Setter Property="Background" Value="#F3F7FD" /> 
     <Setter Property="BorderBrush" Value="#BBD8FB" /> 
     <Setter Property="BorderThickness" Value="1" /> 
     <Setter Property="Cursor" Value="Arrow" /> 
     <Setter Property="Padding" Value="2,1,1,1" /> 
     <Setter Property="Margin" Value="1,0" /> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:TokenItem}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}" 
         Padding="{TemplateBinding Padding}" 
         CornerRadius="0,0,5,5" 
         Margin="{TemplateBinding Margin}" 
         > 
         <StackPanel Orientation="Horizontal" Margin="1" x:Name="myRoot"> 
          <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" /> 

          <Button Margin="3,0,0,0" Cursor="Hand" 
            Command="{x:Static local:TokenizedTextBoxCommands.Delete}" CommandParameter="{TemplateBinding TokenKey}" 
            PresentationTraceSources.TraceLevel="High"> 
         <!--<Button.Template> 
          <ControlTemplate TargetType="Button"> 
           <ContentPresenter /> 
          </ControlTemplate> 
         </Button.Template>--> 
         <Image Source="/Resources/delete8.png" Width="8" Height="8" /> 
        </Button> 
        </StackPanel> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style>  

정적 명령 : 사용자 정의 컨트롤 ItemsControl에서 상속

public static class TokenizedTextBoxCommands 
    { 
    private static RoutedCommand _deleteCommand = new RoutedCommand(); 

    public static RoutedCommand Delete => _deleteCommand; 
    } 

.

public TokenizedTextBox() 
{ 
    CommandBindings.Add(new CommandBinding(TokenizedTextBoxCommands.Delete, DeleteToken, CanDelete)); 
} 

마지막으로 CanDelete 단지 true로 CanExecute을 설정합니다 : - 생략 기능, 서명이

private void CanDelete(object sender, CanExecuteRoutedEventArgs canExecuteRoutedEventArgs) 
    { 
     canExecuteRoutedEventArgs.CanExecute = true; 
    } 

그리고 DeleteToken는 비 정적 생성자에서, 우리는 DeleteToken 방법에 정적 삭제 명령을 연결할 여기 정말에만 중요한 것은 :

private void DeleteToken(object sender, ExecutedRoutedEventArgs e) 
    { 
     ... 
    } 

그래서 희망이 지침/제안을 제공에 관심있는 사람들을위한 충분한 정보입니다. 감사.

답변

0

Pluralsight를 통해 멘토를 고용 했으므로 여기서는별로 관심이 없습니다. 바인딩은 정확했지만 CustomControl에는 마우스 클릭을 캡처하는 RichTextBox가 있습니다. Button의 PreviewMouseDown을 대상으로하는 비헤이비어를 사용하여 문제를 해결했습니다.

관련 문제