Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 将热键指定给菜单项(文件)以将其删除_Xaml_C# 4.0 - Fatal编程技术网

Xaml 将热键指定给菜单项(文件)以将其删除

Xaml 将热键指定给菜单项(文件)以将其删除,xaml,c#-4.0,Xaml,C# 4.0,我在使用热键Alt+F下拉文件菜单时遇到问题。如果按下Alt并释放,然后按F,我可以成功地将其删除,打开菜单,但用Alt按F不起作用。这是我正在使用的代码 < Menu Name="File_Menu" Background="LightGray"> < MenuItem Header="_File" Background="LightGray" Name="File_FileMenu" > 我只想在{}大括号中放置一些代码。如果您查看WPF中MenuItem的Co

我在使用热键Alt+F下拉文件菜单时遇到问题。如果按下Alt并释放,然后按F,我可以成功地将其删除,打开菜单,但用Alt按F不起作用。这是我正在使用的代码

< Menu Name="File_Menu" Background="LightGray">

< MenuItem Header="_File" Background="LightGray" Name="File_FileMenu" > 

我只想在{}大括号中放置一些代码。

如果您查看WPF中
MenuItem
ControlTemplate
,您将看到它使用带有
ItemsControl
弹出式
原语来显示菜单

要手动触发菜单项打开,您需要将弹出窗口设置为true

现在。。。我记不起
IsOpen
属性是否在
MenuItem
上公开,您可能需要遍历可视化树以查找对它的引用

MyVisualTreeHelper的
MyVisualTreeHelper
使用我编写的包装器(就像我之前的许多人一样)快速遍历可视化树。部分内容如下

public static class MyVisualTreeHelper
{
  static bool AlwaysTrue<T>(T obj) { return true; }

  /// <summary>
  /// Finds a parent of a given item on the visual tree. If the element is a ContentElement or FrameworkElement 
  /// it will use the logical tree to jump the gap.
  /// If not matching item can be found, a null reference is returned.
  /// </summary>
  /// <typeparam name="T">The type of the element to be found</typeparam>
  /// <param name="child">A direct or indirect child of the wanted item.</param>
  /// <returns>The first parent item that matches the submitted type parameter. If not matching item can be found, a null reference is returned.</returns>
  public static T FindParent<T>(DependencyObject child) where T : DependencyObject
  {
    return FindParent<T>(child, AlwaysTrue<T>);
  }

  public static T FindParent<T>(DependencyObject child, Predicate<T> predicate) where T : DependencyObject
  {
    DependencyObject parent = GetParent(child);
    if (parent == null)
      return null;

    // check if the parent matches the type and predicate we're looking for
    if ((parent is T) && (predicate((T)parent)))
      return parent as T;
    else
      return FindParent<T>(parent);
  }

  static DependencyObject GetParent(DependencyObject child)
  {
    DependencyObject parent = null;
    if (child is Visual || child is Visual3D)
      parent = VisualTreeHelper.GetParent(child);

    // if fails to find a parent via the visual tree, try to logical tree.
    return parent ?? LogicalTreeHelper.GetParent(child);
  }
}
公共静态类MyVisualTreeHelper
{
静态bool-AlwaysTrue(T-obj){返回真;}
/// 
///在可视树上查找给定项的父项。如果该元素是ContentElement或FrameworkElement
///它将使用逻辑树来跳转间隙。
///如果找不到匹配项,则返回空引用。
/// 
///要查找的元素的类型
///所需物品的直接或间接子项。
///与提交的类型参数匹配的第一个父项。如果找不到匹配的项,则返回空引用。
公共静态T FindParent(DependencyObject子对象),其中T:DependencyObject
{
返回FindParent(子女,始终为父母);
}
公共静态T FindParent(DependencyObject子级,谓词谓词),其中T:DependencyObject
{
DependencyObject父对象=GetParent(子对象);
如果(父项==null)
返回null;
//检查父项是否与我们要查找的类型和谓词匹配
if((父项是T)和&(谓词((T)父项)))
将父对象返回为T;
其他的
返回FindParent(父级);
}
静态DependencyObject GetParent(DependencyObject子对象)
{
DependencyObject父对象=null;
if(子对象是可视的| |子对象是可视的3D)
父级=VisualTreeHelper.GetParent(子级);
//如果无法通过可视树找到父级,请尝试使用逻辑树。
返回父级??LogicalTreeHelper.GetParent(子级);
}
}

HTH,

提示:使用代码格式使您的问题更具可读性。谢谢Dennis。。但我觉得还有其他问题。。无论如何,谢谢你。。
public static class MyVisualTreeHelper
{
  static bool AlwaysTrue<T>(T obj) { return true; }

  /// <summary>
  /// Finds a parent of a given item on the visual tree. If the element is a ContentElement or FrameworkElement 
  /// it will use the logical tree to jump the gap.
  /// If not matching item can be found, a null reference is returned.
  /// </summary>
  /// <typeparam name="T">The type of the element to be found</typeparam>
  /// <param name="child">A direct or indirect child of the wanted item.</param>
  /// <returns>The first parent item that matches the submitted type parameter. If not matching item can be found, a null reference is returned.</returns>
  public static T FindParent<T>(DependencyObject child) where T : DependencyObject
  {
    return FindParent<T>(child, AlwaysTrue<T>);
  }

  public static T FindParent<T>(DependencyObject child, Predicate<T> predicate) where T : DependencyObject
  {
    DependencyObject parent = GetParent(child);
    if (parent == null)
      return null;

    // check if the parent matches the type and predicate we're looking for
    if ((parent is T) && (predicate((T)parent)))
      return parent as T;
    else
      return FindParent<T>(parent);
  }

  static DependencyObject GetParent(DependencyObject child)
  {
    DependencyObject parent = null;
    if (child is Visual || child is Visual3D)
      parent = VisualTreeHelper.GetParent(child);

    // if fails to find a parent via the visual tree, try to logical tree.
    return parent ?? LogicalTreeHelper.GetParent(child);
  }
}