PreviewCanExecuteEvent 未执行
我的 WPF 程序有问题。 我正在尝试创建一个对象,它将向同一范围内的所有控件添加处理程序。
以下行不起作用。 该事件未得到处理。
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true);
我还有一个绑定到按钮的命令。 所以我的想法是我希望命令的 CanExecute 运行:这工作正常。 我还想要一个 PreviewCanExecute 的处理程序:这不起作用。
很抱歉我无法更好地解释。
请参阅下面的代码:
XAML:
<Window.Resources>
<my:PermissionScope x:Key="permissionManager"/>
</Window.Resources>
<StackPanel>
<TextBox Height="23" Name="textBox1" Width="120" />
<Button Content="Permission Required" Command="{Binding Path=PermissionRequired}" my:PermissionScope.SharedPermissionScope="{StaticResource permissionManager}"/>
<Button Content="Permission not required" Command="{Binding Path=PermissionRequired}"/>
</StackPanel>
PermissionScope.cs
public class PermissionScope
{
public static readonly DependencyProperty SharedPermissionScopeProperty =
DependencyProperty.RegisterAttached("SharedPermissionScope", typeof(PermissionScope), typeof(PermissionScope),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits,
new PropertyChangedCallback(OnUseGlobalSharedPermissionScopeChanged)));
public static void SetSharedPermissionScope(DependencyObject depObj, PermissionScope scope)
{
// never place logic in here, because these methods are not called when things are done in XAML
depObj.SetValue(SharedPermissionScopeProperty, scope);
}
public static PermissionScope GetSharedPermissionScope(DependencyObject depObj)
{
// never place logic in here, because these methods are not called when things are done in XAML
return depObj.GetValue(SharedPermissionScopeProperty) as PermissionScope;
}
private static void OnUseGlobalSharedPermissionScopeChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
{
if (depObj is Button)
{
if (args.OldValue != null)
{
RemoveEventHandlers(depObj as UIElement, args.OldValue as PermissionScope);
}
if (args.NewValue != null)
{
AttachEventHandlers(depObj as UIElement, args.NewValue as PermissionScope);
}
}
}
private static void AttachEventHandlers(UIElement element, PermissionScope scope)
{
if (element != null && scope != null)
{
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true); // we need to see all events to subvert the built-in undo/redo tracking in the text boxes
}
}
private static void RemoveEventHandlers(UIElement element, PermissionScope scope)
{
if (element != null && scope != null)
{
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler));
}
}
private void CanExecutedHandler(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Command is CommandBase)
{
bool hasPermission = false;
hasPermission = ((CommandBase)e.Command).HasPermission();
ShowControl((UIElement)e.OriginalSource, hasPermission);
}
}
public static void ShowControl(UIElement element, bool show)
{
element.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
}
}
我真的没有任何帮助。
此致, 迈克尔
I have a problem with my WPF program. I'm trying to create an object that will add handlers to all controls in the same scope.
The following line does not work. The event is not handled.
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true);
I also have a Command binded to the button. So the idea is that i want CanExecute of the Command to run: This works fine. I also want a handler for the PreviewCanExecute: This does not work.
I'm sorry that I can not explain any better.
See my code below:
XAML:
<Window.Resources>
<my:PermissionScope x:Key="permissionManager"/>
</Window.Resources>
<StackPanel>
<TextBox Height="23" Name="textBox1" Width="120" />
<Button Content="Permission Required" Command="{Binding Path=PermissionRequired}" my:PermissionScope.SharedPermissionScope="{StaticResource permissionManager}"/>
<Button Content="Permission not required" Command="{Binding Path=PermissionRequired}"/>
</StackPanel>
PermissionScope.cs
public class PermissionScope
{
public static readonly DependencyProperty SharedPermissionScopeProperty =
DependencyProperty.RegisterAttached("SharedPermissionScope", typeof(PermissionScope), typeof(PermissionScope),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits,
new PropertyChangedCallback(OnUseGlobalSharedPermissionScopeChanged)));
public static void SetSharedPermissionScope(DependencyObject depObj, PermissionScope scope)
{
// never place logic in here, because these methods are not called when things are done in XAML
depObj.SetValue(SharedPermissionScopeProperty, scope);
}
public static PermissionScope GetSharedPermissionScope(DependencyObject depObj)
{
// never place logic in here, because these methods are not called when things are done in XAML
return depObj.GetValue(SharedPermissionScopeProperty) as PermissionScope;
}
private static void OnUseGlobalSharedPermissionScopeChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
{
if (depObj is Button)
{
if (args.OldValue != null)
{
RemoveEventHandlers(depObj as UIElement, args.OldValue as PermissionScope);
}
if (args.NewValue != null)
{
AttachEventHandlers(depObj as UIElement, args.NewValue as PermissionScope);
}
}
}
private static void AttachEventHandlers(UIElement element, PermissionScope scope)
{
if (element != null && scope != null)
{
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true); // we need to see all events to subvert the built-in undo/redo tracking in the text boxes
}
}
private static void RemoveEventHandlers(UIElement element, PermissionScope scope)
{
if (element != null && scope != null)
{
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler));
}
}
private void CanExecutedHandler(object sender, CanExecuteRoutedEventArgs e)
{
if (e.Command is CommandBase)
{
bool hasPermission = false;
hasPermission = ((CommandBase)e.Command).HasPermission();
ShowControl((UIElement)e.OriginalSource, hasPermission);
}
}
public static void ShowControl(UIElement element, bool show)
{
element.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
}
}
I really not some help.
Best regards,
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了我自己。 这仅适用于 RoutedCommands,不适用于 ICommand
I found out my self. This will only work with RoutedCommands and not ICommand