2009-07-09 2 views
6

여러 창이있는 WPF 응용 프로그램이 있습니다. GLOBAL inputBindings를 정의하고 싶습니다.XAML - 전역 inputBinding을 갖는 방법?

LOCAL 입력 바인딩을 정의하려면 Window.InputBindings 또는 UserControl.InputBindings에서 입력을 선언하기 만하면됩니다. 내가 2 개 개의 다른 창에서 바인딩 같은 경우

<Application 
....> 
<Application.InputBindings> 
... 
</Application.InputBindings> 

... 내가 응용 프로그램 클래스와 동일한 기능을 수행 할 수 있으면 좋겠다, 전역을 정의하려면, 내가 두 번 코딩해야합니다. 이것은 D.R.Y.의 철학을 충족하지 않으며 더 나은 방법이있는 것 같아요 ...

편집 : 그의 대답은 Kent Boogaart 스타일을 사용하는 것이 좋습니다. 불행히도, 나는 그것을 정의하는 방법을 알아낼 수 없습니다. 오류 MC3080 :이 접근 set 접근을 가지고 있지 않기 때문에 재산권 세터 '있는 InputBindings'을 설정할 수 없습니다 그것은 오류가 발생합니다

<Application.Resources> 
    <Style TargetType="Window"> 
     <Setter Property="InputBindings"> 
      <Setter.Value> 
       <Window.InputBindings> 
        <KeyBinding KeyGesture="Ctrl+M" Command="local:App.MsgCommand /> 
       </Window.InputBindings> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Application.Resources> 

:이 코드입니다.

내 스타일이 잘못 되었나요? 다른 해결책이 있습니까?

아이디어가 있으십니까? 감사!

답변

9

Attached PropertyStyle을 사용하여 응용 프로그램에서 주어진 유형의 모든 컨트롤에 InputBindings을 설정하는 것이 한 가지 해결책입니다. 불행하게도, "캐치 올 (do all-catch)"을 할 수 없으므로 Style (내가 알고있는 것은 어쨌든), InputBindings을 설정할 각 컨트롤 유형에 대해 Style을 생성해야합니다. 그러나 너무 많은 컨트롤이 필요함). - 위의 코드를 사용할 때 나는 아주 이상한 문제가 있었다

using System.Windows; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    public class MyAttached 
    { 
     public static readonly DependencyProperty InputBindingsProperty = 
      DependencyProperty.RegisterAttached("InputBindings", typeof(InputBindingCollection), typeof(MyAttached), 
      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); 
     } 

    } 
} 

<Application x:Class="WpfApplication1.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:loc="clr-namespace:WpfApplication1" 
    StartupUri="Window1.xaml"> 
    <Application.Resources> 
     <Style TargetType="TextBox"> 
      <Setter Property="loc:MyAttached.InputBindings"> 
       <Setter.Value> 
        <InputBindingCollection> 
         <KeyBinding Key="A" Modifiers="Ctrl" Command="loc:Window1.MyAction" /> 
        </InputBindingCollection> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <Style TargetType="Button"> 
      <Setter Property="loc:MyAttached.InputBindings"> 
       <Setter.Value> 
        <InputBindingCollection> 
         <KeyBinding Key="A" Modifiers="Ctrl" Command="loc:Window1.MyAction" /> 
        </InputBindingCollection> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Application.Resources> 
</Application> 

<Window x:Class="WpfApplication1.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:loc="clr-namespace:WpfApplication1" 
    Title="Window1" Height="300" Width="300"> 
    <Window.CommandBindings> 
     <CommandBinding Command="loc:Window1.MyAction" Executed="MyAction_Executed" /> 
    </Window.CommandBindings> 
    <StackPanel> 
     <Button Content="Try Ctrl+A Here!" /> 
     <TextBox Text="Try Ctrl+A Here!" /> 
    </StackPanel> 
</Window> 

using System.Windows; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    public partial class Window1 
    { 
     public static readonly RoutedUICommand MyAction = new RoutedUICommand("MyAction", "MyAction", typeof(Window1)); 

     public Window1() { InitializeComponent(); } 

     private void MyAction_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("MyAction!"); } 
    } 
} 
+0

주의 : 다음은이 작업을 수행하는 방법을 보여줍니다 일부 샘플 코드입니다. 그것은 스타일에 적용하기 전까지는 훌륭하게 작동했습니다. 무작위로 작동하는 것처럼 보이지만 DataContext는 null이됩니다 (따라서 아무 것도 바인딩하지 않습니다). 나는 이것이 왜 무작위로 일어날 지 알 수 없었다. – Asheh

0

모든 Window에 적용되는 Style을 만들 수 있습니다. 그 StyleInputBindings을 설정할 수 있습니다.