2011-03-24 70 views
3

我目前正在使用我的系統托盤圖標的上下文菜單中的文本框。
問題是,文本框沒有對keydown事件做出反應。這意味着我無法將文本插入到我的文本框中。C#WPF MVVM - 系統托盤圖標中的文本框contextmenu


<tb:TaskbarIcon x:Name="NotifyIcon" ToolTip="App" IconSource="/Images/MyIcon.ico" > 
    <tb:TaskbarIcon.ContextMenu> 
     <ContextMenu MaxWidth="180"> 
      <MenuItem Width="auto" Header="Template"> 
       <MenuItem.HeaderTemplate> 
        <DataTemplate> 
         <StackPanel Width="auto" Height="auto" Orientation="Horizontal" > 
          <TextBox Height="20" Text="{Binding Initial.textBoxText, Source={StaticResource Locator}, Mode=TwoWay}" HorizontalAlignment="Left" 
               Name="txtNumberFromTrail" VerticalAlignment="Center" Width="105" > 
           <i:Interaction.Triggers> 
            <i:EventTrigger EventName="KeyDown"> 
             <cmd:EventToCommand Command="{Binding Initial.KeyDown, Source={StaticResource Locator}}" 
                     PassEventArgsToCommand="True" /> 
            </i:EventTrigger> 
           </i:Interaction.Triggers> 
          </TextBox> 
         </StackPanel> 
        </DataTemplate> 
       </MenuItem.HeaderTemplate> 
      </MenuItem> 
     </ContextMenu> 
    </tb:TaskbarIcon.ContextMenu> 
</tb:TaskbarIcon> 
+1

你想要哪個鍵來記錄?普通文本鍵或特殊鍵(例如箭頭鍵)? – thumbmunkeys 2011-05-28 07:18:08

回答

0

假設「初始」是ViewModelLocator(定位),它返回的視圖模型的參考屬性,這裏是你如何定義的視圖模型的命令:

private RelayCommand<KeyEventArgs> _KeyDown; 
    public RelayCommand<KeyEventArgs> KeyDown 
    { 
     get 
     { 
      if (_KeyDown == null) 
      { 
       _KeyDown = new RelayCommand<KeyEventArgs>(delegate(KeyEventArgs e) 
       { 
        //Functionality that you need to perform on this event  
       }); 
      } 
      return _KeyDown; 
     } 
    } 

出現您的XAML對我好。如果你像上面那樣定義命令,希望它能起作用。

0

如果您在對焦文本框時遇到困難,這是因爲您沒有激活文本框控件所屬的窗口線程。檢查下面的代碼。快樂的編碼。

[DllImport("USER32.DLL")] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

和...

tb.ShowCustomBalloon((UIElement)balloon, System.Windows.Controls.Primitives.PopupAnimation.Scroll, null); 

HwndSource source = (HwndSource)PresentationSource.FromVisual(balloon); 
IntPtr handle = source.Handle; 

SetForegroundWindow(handle);