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