2013-01-02 2 views
0

잘 알려진 Calculator 예제의 자체 버전을 구현하여 MEF 프레임 워크로 작업하는 것을 익히려고합니다. 사용자 인터페이스는 WPF입니다.합성 후 창에 InputBindings를 추가하는 방법은 무엇입니까?

Viewmodel은보기에서 의 'ListBox'로 표시되는 ObservableCollection(Of IOperation)을 보유하고 있습니다. 각 Button의 텍스트는 Symbol이라는 속성으로 IOperation에 정의 된 Char입니다. ListBox의 SelectedItem을 ViewModel의 속성에 바인딩하면 Button을 눌렀을 때 현재 IOperationCalculate 메서드를 실행할 수 있습니다. (아래 코드는 이것을 설명.)

을하지만, 지금은 각 KeyBindingIOperation에 정의 된 Symbol와 연결됩니다보기에 InputBindings를 추가해야합니다. (switch) 문을 구현하여 Viewmodel의 IOperation 콜렉션을 반복하여 'Calculate'메소드를 호출해야하는 것을 선택하는 것을 피할 수없는 것 같습니다.

아이디어가 있으십니까?

XAML :

<ListBox Grid.Column="1" Grid.Row="3" Name="OperationsList" 
     SelectedItem="{Binding ActiveOperation,Mode=TwoWay}" 
     ItemsSource="{Binding Operations}"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid IsItemsHost="True"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Button Content="{Binding Symbol}" 
         ToolTip="{Binding Description}" 
         Command="{Binding ElementName=OperationsList, Path=DataContext.ActivateOperation}" 
          Click="Button_Click_1"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

IOPERATION :

Public Interface IOperation 
    Function Calculate() As Double 
    Property Left As Double 
    Property Right As Double 
    Property Symbol As String 
    Property Description As String 
End Interface 

의 ViewModel : 가장자리가 있으므로 적절하게 조절에 대해

Private _activateOperation As Command 
    Public Property ActivateOperation As Command 
     Get 
      Return _activateOperation 
     End Get 
     Set(value As Command) 
      _activateOperation = value 
      OnPropertyChanged() 
     End Set 
    End Property 

Public Property ActiveOperation As IOperation 
    Get 
     Return _compositor.ActiveOperation 
    End Get 
    Set(value As ICalculator) 
     _compositor.ActiveOperation = value 
     OnPropertyChanged() 
    End Set 
End Property 

    Public ReadOnly Property Operations As ObservableCollection(Of IOperation) 
     Get 
      Return New ObservableCollection(Of ICalculator)(_compositor.Operations) 
     End Get 
    End Property 

    Private Sub DoActivateOperation(Optional parameter As Object = Nothing) 
     If Left.HasValue Then 
      MakeCalculation() 
     Else 
      Left = CDbl(Display) 
      ClearDisplay() 
     End If 
    End Sub 

답변

1

코드이라도 거친입니다 ...

// in ViewModel, after composition 

// map string symbols to input Keys (for brevity, using a Dictionary) 
Dictionary<string, System.Windows.Input.Key> SymbolsToKeys = new Dictionary<string, Key> 
{ 
    { "+", Key.Add }, 
    // etc. 
} 

// compose the list (expose it via a public property) 
// (ensure not to get confused: the KeyValuePair.Key is for the string symbol 
// ... and the KeyValuePair.Value is for the Sys.Win.Input.Key value) 
KeyBindings = (from symbolAndKey in SymbolsToKeys 
       join op in Operations on Equals(symbolAndKey.Key, op.Symbol) 
       select new KeyBinding(
       new Command(op.Calculate)), 
       new KeyGesture(symbolAndKey.Value) 
      ).ToList(); 

// in View, after Binding to ViewModel 
var vm = DataContext as YourViewModel; 
if(vm != null) 
{ 
    foreach(var keybinding in vm.KeyBindings){ 
     this.InputBindings.Add(keybinding); 
    } 
} 
+0

감사합니다. 그러나 KeyBindings는 일반적으로 Ctrl 키의 Alt 키를 눌러야하기 때문에 대부분의 기호에는 작동하지 않습니다. – Dabblernl