2011-02-16 3 views
1

Caliburn.Micro를 사용하는 .NET 4.0 응용 프로그램이 있습니다. 각 메뉴 항목에 대해 XAML 코드를 작성할 필요가없는 동적 메뉴를 만들고 싶습니다. 또한 각 명령을 주요 제스처와 연결하려고합니다. 다음과 같이 내가 행동에 내 도구 모음을 결합주요 제스처가있는 동적 메뉴

private List<IAction> _actions; 
public List<IAction> Actions 
{ 
    get { return _actions; } 
    set 
    { 
     _actions = value; 
     NotifyOfPropertyChange(()=> Actions); 
    } 
} 

:

내 뷰 모델에서
public interface IAction 
{ 
    string Name { get; } 
    InputGesture Gesture { get; } 
    ICommand Command { get; }  
} 

내가 IActions의 목록을 노출 :

나는 인터페이스 IAction이

<ToolBar> 
    <Menu ItemsSource="{Binding Actions}"> 
     <Menu.ItemContainerStyle> 
      <Style TargetType="MenuItem"> 
       <Setter Property="Header" Value="{Binding Name}" /> 
       <Setter Property="Command" Value="{Binding Command}" /> 
      </Style> 
     </Menu.ItemContainerStyle> 
    </Menu> 
</ToolBar> 

위의 모든 작업이 가능합니다.

누락 된 부분은 주요 제스처의 데이터 바인딩입니다.

모든 지역 나는 읽고 난 단지 같은 Window.InputBindings의 정적 정의와 예를 찾을 수 : 단순히 ItemsControl에있는 Window.InputBindings을 캡슐화 할 수 있다면

<Window.InputBindings> 
    <KeyBinding Key="B" Modifiers="Control" Command="ApplicationCommands.Open" /> 
</Window.InputBindings> 

그것은 좋은 것입니다,하지만 그 '아무튼 일하지 마라.

여러분 중 누구라도 Window.InputBindings를 동적으로 바인딩하는 방법을 알고 있습니까?

감사합니다.

답변

2

창 개체에 키 제스처를 만들어야합니다 (창 전체에 적용해야하는 경우).

예를 들어 BindableInputBindings이라는 이름의 종속성 속성을 가진 사용자 지정 파생 창 개체를 만들 수 있습니다. OnChanged 콜백의이 속성은 소스 모음이 변경 될 때마다 키 바인딩을 추가/제거합니다.

EDIT : 약간의 오차가있을 수 있습니다.

public class WindowWithBindableKeys: Window { 

    protected static readonly DependencyProperty BindableKeyBindingsProperty = DependencyProperty.Register(
     "BindableKeyBindings", typeof(CollectionOfYourKeyDefinitions), typeof(WindowWithBindableKeys), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnBindableKeyBindingsChanged)) 
    ); 

    public CollectionOfYourKeyDefinitions BindableKeyBindings 
    { 
     get 
     { 
      return (string)GetValue(BindableKeyBindingsProperty); 
     } 
     set 
     { 
      SetValue(BindableKeyBindingsProperty, value); 
     } 
    } 

    private static void OnBindableKeyBindingsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     (d as WindowWithBindableKeys).InputBindings.Clear(); 

     // add the input bidnings according to the BindableKeyBindings 
    } 

} 

다음 XAML

의 빠른 회신
<mynamespace:WindowWithBindableKeys BindableKeyBindings={Binding YourSourceOfKeyBindings} ... > ... 
+0

감사합니다! 나는 당신이 묘사하고있는 것에 대한 코드 예제를 매우 고맙게 생각합니다. –

+0

@ROBOlav 편집 됨 –

+0

와우. 환상적. 그것은 일했다 :) 고마워! –