previewCaneXecuteevent가 실행되지 않았습니다
-
10-07-2019 - |
문제
WPF 프로그램에 문제가 있습니다. 동일한 범위에서 모든 컨트롤에 핸들러를 추가 할 객체를 만들려고합니다.
다음 줄은 작동하지 않습니다. 이벤트는 처리되지 않습니다.
element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true);
나는 또한 버튼에 명령을 내린다. 따라서 아이디어는 명령을 실행하기를 원한다는 것입니다. 이것은 잘 작동합니다. 또한 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>
fermissionscope.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;
}
}
나는 정말로 도움이되지 않습니다.
안부, 마이클
해결책
나는 내 자신을 알았다. 이것은 Icommand가 아닌 RoutedCommands에서만 작동합니다
제휴하지 않습니다 StackOverflow