這是我實現這個方式:
我創建了一個從WindowsFormsHost
繼承
public class MyWpfControl: WindowsFormsHost
{
private MyWindowsFormsControl _winControl = new MyWindowsFormsControl();
public MyWpfControl()
{
_winControl.KeyDown += _winControl_KeyDown;
}
void _winControl_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift)
{
MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
}
else if (e.KeyCode == Keys.Tab)
{
MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
}
}
完全爲我工作
使用相同的方法,你也可以讓你的窗口控制的控制有需要的wpf數據綁定:
public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent("SelectionChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyWpfControl));
public event RoutedEventHandler SelectionChanged
{
add { AddHandler(SelectionChangedEvent, value); }
remove { RemoveHandler(SelectionChangedEvent, value); }
}
void RaiseSelectionChangedEvent()
{
var newEventArgs = new RoutedEventArgs(SelectionChangedEvent);
RaiseEvent(newEventArgs);
}
private void InitDependencyProperties()
{
_winControl.EditValueChanged += (sender, e) =>
{
SetValue(SelectedValueProperty, _winControl.EditValue);
if (!_disabledSelectionChangedEvent)
{
RaiseSelectionChangedEvent();
}
};
}
public static readonly DependencyProperty SelectedValueProperty = DependencyProperty.Register("SelectedValue", typeof(string), typeof(MyWpfControl),
new PropertyMetadata("",
(d, e) =>
{
var myControl = d as MyWpfControl;
if (myControl != null && myControl._brokersCombo != null)
{
var val = myControl.GetValue(e.Property) ?? string.Empty;
myControl._winControl.EditValue = val;
}
}, null));
這裏是XAML:
<u:MyWpfControl x:Name="myWpfControl" Margin="5,0,0,0" DataSource="{Binding BindingData, UpdateSourceTrigger=PropertyChanged}" SelectedValue="{Binding SelectedPropertyNameOnViewModel, Mode=TwoWay}">
</u:MyWpfControl>
在真正的項目導致我這個最小化的問題,情況甚至有點不同。有所有WPF控件(包括WindowsFormsHost)之間的Tab鍵切換。但是WindowsFormsHost中的Tab鍵並沒有轉到WindowsFormsHost中的其他WinForms控件之一,它只是讓WindowsFormsHost繼續使用下一個WPF控件。 – ZoolWay 2011-03-03 10:39:44