您能否提供一個樣本,介紹如何實現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. 我可以在獨立的XAML文件中使用自定義控件嗎?
- 2. 如何實現自定義WPF控件
- 3. xaml(wpf)如何解決我的自定義窗口模板中的控件?
- 4. 如何使用C#在我的XSLT文件中呈現自定義XSL控件?
- 5. 如何在XAML模板中使用WPF自定義控件屬性?
- 6. 如何在另一個項目中使用自定義WPF/XAML控件?
- 7. 我如何處理c#wpf自定義控件中的事件
- 8. 在xaml中使用自定義控件中的C#變量(silverlight)
- 9. 我在哪裏以及如何在我的XAML中實現ScrollViewer?
- 10. 我可以可靠地使用winform中的WPF自定義控件嗎?
- 11. WPF創建在xaml中定義的控件的實例
- 12. 如何在XAML和WPF中實現可拆卸的控件
- 13. 使用WPF中的命令實現自定義CanExecuteChanged事件
- 14. 如何在WPF中使用自定義控件
- 15. 如何在自定義WebAuthenticationDetails中使用我的自定義ConfigurationProperties?
- 16. ColorAnimation不喜歡我在我的WPF xaml文件中使用DynamicResource
- 17. WPF自定義控件XAML null錯誤
- 18. 在自定義控件中實現ClickHandler
- 19. 爲什麼我不能在c#中使用我自定義的WPF控件代碼
- 20. 如何使自定義XAML用戶控件可綁定?
- 21. 在ASP.net中使用自定義控件的WPF類屬性
- 22. 如何在數據綁定WPF中的TabControl時使用自定義TabItem控件?
- 23. 在WPF中,如何給我的自定義控件設計模式中使用的默認樣式?
- 24. 如何使用可可實現時間線自定義控件?
- 25. 如何使我的webbrowser能夠在Java中使用JavaScript?
- 26. 在WPF中應該將XAML用於自定義控件的佈局?創建自定義的控制有
- 27. 如何實現MVVM自定義控件?
- 28. 在WPF XAML中,如何連接2個常量,以便我可以使用預定義的路徑?
- 29. 如何在我自己的控件中實現Display/ValueMember
- 30. 自定義WPF控件是否可以實現IsDefault屬性
+1,很好的例子 – Mizipzor 2010-08-17 12:44:51
很好的例子!謝謝! – Vitalij 2010-08-17 13:08:59