2010-04-18 5 views
18

MVVM 디자인 패턴을 사용하여 WPF 응용 프로그램을 만들고 사용자가 마우스 가운데 버튼을 클릭 할 때 탭을 닫을 수 있도록 TabItem 컨트롤을 확장하려고합니다. 내가 InputBindings를 사용하여 이것을 달성하기 위해 노력하고있어, 스타일 내에서 그것을 정의하려고 시도하기 전까지는 아주 잘 작동합니다. DependencyProperty를 사용하여 첨부하지 않는 한 스타일에 InputBinding을 추가 할 수 없다는 것을 배웠습니다. 그래서 나는 비슷한 게시물 here...을 따라 갔고 거의 작동합니다 ... 거의. 마우스 가운데 버튼을 사용하여 하나의 탭을 닫을 수 있지만 다른 탭에서는 작동하지 않습니다. 모든 탭은 런타임에 추가되고 같은 스타일을 상속합니다.스타일 내 InputBinding 정의

그래서 도움이 필요합니다. 왜 이것이 처음으로 일하는 것이지, 후에 만하는 것이 아니겠습니까? 분명히 TabItem을 상속 한 사용자 정의 컨트롤을 만들어 작동시킬 수는 있지만이 문제를 내 프로젝트에서 확장 된 것으로 볼 수 있기 때문에이를 파악하고 싶습니다. DependencyProperties에 대한 전문가가 아니므로 나를 도와주십시오. 감사!

스타일 :

<Style TargetType="{x:Type TabItem}"> 
    <Setter Property="w:Attach.InputBindings"> 
     <Setter.Value> 
      <InputBindingCollection> 
       <MouseBinding MouseAction="MiddleClick" 
           Command="{Binding CloseCommand}"/> 
      </InputBindingCollection> 
     </Setter.Value> 
    </Setter> 
    ... 
</Style> 

클래스

public class Attach 
{ 
    public static readonly DependencyProperty InputBindingsProperty = 
     DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(Attach), 
     new FrameworkPropertyMetadata(new InputBindingCollection(), 
     (sender, e) => 
     { 
      var element = sender as UIElement; 
      if (element == null) return; 
      element.InputBindings.Clear(); 
      element.InputBindings.AddRange((InputBindingCollection)e.NewValue); 
     })); 

    public static InputBindingCollection GetInputBindings(UIElement element) 
    { 
     return (InputBindingCollection)element.GetValue(InputBindingsProperty); 
    } 

    public static void SetInputBindings(UIElement element, InputBindingCollection inputBindings) 
    { 
     element.SetValue(InputBindingsProperty, inputBindings); 
    } 
} 
+0

Daniel이 제안한 코드를 사용하여도 위에서 설명한 정확한 문제가 발생했습니다. 위의 Attach 클래스를 사용할 때, 특히 스타일에 이상한 것이 있습니다. 나는 DataContext가 "때때로"InputBindings가 추가되었을 때 null이라는 것을 알았습니다. 그래서 바인딩이 발생했을 때 명령을 찾을 수 없었습니다. 솔루션을 찾지 못했지만 아래의 답변 에서처럼 바인딩을 복제하는 결과가 발생했습니다. – Asheh

답변

14

나 자신을 알아 낸, 신경 쓰지 마. 결국 위의 Attach 클래스를 사용하지 않게되었습니다. 대신 TabItem (테두리) 인 ControlTemplate에 InputBindings를 사용 했으므로 다음과 같이 보입니다 ... 왜 내가 생각하지 않았는지 모르겠습니다. 첫 번째 장소에서 이것의 : ..

<ControlTemplate TargetType="{x:Type TabItem}"> 
    <Grid SnapsToDevicePixels="true"> 
     <Border x:Name="Bd" ...> 
      <DockPanel> 
       ... 
      </DockPanel> 
      <Border.InputBindings> 
       <MouseBinding MouseAction="MiddleClick" 
           Command="{Binding CloseCommand}"/> 
      </Border.InputBindings> 
     </Border> 
    </Grid> 
    ... 
</ControlTemplate> 
+0

내가 만든 모든 템플릿을 대체해야하기 때문에이 솔루션이 마음에 들지 않습니다 ... –

14

당신의 클래스 "Attach"는 나를 위해 잘 작동했다! 공유 수정 : 사람이 필요로하는 경우 , 트릭은 다음과 같이 사용 스타일은 X와 함께입니다

<InputBindingCollection x:Key="inputCollection" x:Shared="False"> 
     <KeyBinding Key="Del" Command="{Binding DeleteItemCommand}"/> 
</InputBindingCollection> 

<Style TargetType="{x:Type TabItem}"> 
    <Setter Property="w:Attach.InputBindings" Value="{StaticResource inputCollection}" /> 
    ... 
</Style> 

감사합니다!