您能否提供一個樣本,介紹如何實現ICommandSource
接口。因爲我希望我的UserControl
不具備在xaml中指定命令的能力,所以擁有此能力。並且能夠在用戶點擊CustomControl
時處理該命令。在WPF中,我如何實現ICommandSource以使我的自定義控件能夠使用xaml中的Command?
9
A
回答
18
下面是一個例子:
public partial class MyUserControl : UserControl, ICommandSource
{
public MyUserControl()
{
InitializeComponent();
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(MyUserControl), new UIPropertyMetadata(null));
public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}
// Using a DependencyProperty as the backing store for CommandParameter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(MyUserControl), new UIPropertyMetadata(null));
public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(CommandTargetProperty); }
set { SetValue(CommandTargetProperty, value); }
}
// Using a DependencyProperty as the backing store for CommandTarget. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandTargetProperty =
DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(MyUserControl), new UIPropertyMetadata(null));
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
var command = Command;
var parameter = CommandParameter;
var target = CommandTarget;
var routedCmd = command as RoutedCommand;
if (routedCmd != null && routedCmd.CanExecute(parameter, target))
{
routedCmd.Execute(parameter, target);
}
else if (command != null && command.CanExecute(parameter))
{
command.Execute(parameter);
}
}
}
注意,CommandTarget
財產僅用於RoutedCommands
0
您的UserControl將有一個代碼位於文件cs或vb後面,您必須實現接口ICommandSource,並且一旦實現了該接口,在某些情況下,您將不得不調用該命令並檢查CanExecute。
相關問題
- 1. 如何讓WPF在我的視圖中實例化一個自定義控件,在我的XAML中使用另一個自定義控件基類?
- 2. 我可以在獨立的XAML文件中使用自定義控件嗎?
- 3. 如何實現自定義WPF控件
- 4. xaml(wpf)如何解決我的自定義窗口模板中的控件?
- 5. 如何使用C#在我的XSLT文件中呈現自定義XSL控件?
- 6. 如何在XAML模板中使用WPF自定義控件屬性?
- 7. 如何在另一個項目中使用自定義WPF/XAML控件?
- 8. 我如何處理c#wpf自定義控件中的事件
- 9. 我在哪裏以及如何在我的XAML中實現ScrollViewer?
- 10. 在xaml中使用自定義控件中的C#變量(silverlight)
- 11. 我可以可靠地使用winform中的WPF自定義控件嗎?
- 12. 如何在XAML和WPF中實現可拆卸的控件
- 13. 如何在ImageSource的自定義wpf控件上實現DependencyProperty?
- 14. WPF創建在xaml中定義的控件的實例
- 15. 使用WPF中的命令實現自定義CanExecuteChanged事件
- 16. 如何在WPF中使用自定義控件
- 17. 如何在自定義WebAuthenticationDetails中使用我的自定義ConfigurationProperties?
- 18. ColorAnimation不喜歡我在我的WPF xaml文件中使用DynamicResource
- 19. WPF自定義控件XAML null錯誤
- 20. 在xaml/wpf中自定義我的ScrollViewer /滾動條
- 21. 在自定義控件中實現ClickHandler
- 22. 我應該如何使用Spring的AbstractAnnotationConfigDispatcherServletInitializer實現自定義SiteMeshFilter?
- 23. WPF - 不能使用自定義控制
- 24. 我可以在WinForms中使用WPF控件來實現透明效果嗎?
- 25. 爲什麼我不能在c#中使用我自定義的WPF控件代碼
- 26. 如何在XAML中爲不同的控件使用自定義樣式
- 27. 如何使用自己的DataTemplate DependencyProperty實現WPF控件?
- 28. 如何使自定義XAML用戶控件可綁定?
- 29. 我們如何在自定義標籤中使用現有的taglib功能?
- 30. 在ASP.net中使用自定義控件的WPF類屬性
+1,很好的例子 – Mizipzor 2010-08-17 12:44:51
很好的例子!謝謝! – Vitalij 2010-08-17 13:08:59