一、界面内容(部分:仅供参考)

<Window>
<Window.Resources> <!--工具数据源-->
<XmlDataProvider x:Key="toolsDS" Source="Config\Tools\Tools.xml" XPath="Tools/Tool"></XmlDataProvider>
<!--Tool模板-->
<HierarchicalDataTemplate DataType="Tool" ItemsSource="{Binding XPath=Tool}" >
<StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="0,10,0,0" > <!--MouseEnter="MenuItemImage_MouseEnter" MouseLeave="MenuItemImage_MouseLeave"-->
<TextBlock Tag ="{Binding XPath=@Name}" Width="" Height="" Margin="0,0,0,0" VerticalAlignment="Center" >
<Image x:Name="img_menuIcon" MouseEnter="MenuItemImage_MouseEnter" MouseLeave="MenuItemImage_MouseLeave" Source="{Binding XPath=@ImagePath0}" ><!--Width="" Height=""--></Image>
</TextBlock>
<TextBlock x:Name="img_Title" Text ="{Binding XPath=@Title}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" Margin="0,3,0,0" Foreground="#FFD1D1D1" FontSize="{Binding XPath=@FontSize}" MouseEnter="MenuItemText_MouseEnter" MouseLeave="MenuItemText_MouseLeave" >
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate> </Window.Resources> <StackPanel x:Name="StackBlist" Orientation="Vertical" MenuItem.Click="MenuItem_Click" VerticalAlignment="Top" Background="#43464f">
<Button x:Name="btnOS" Click="btnOS_Click" Width="" Height="" Margin="-2,-2,-2,0" Background="#FF939393" BorderBrush="{x:Null}" Foreground="{x:Null}" BorderThickness=""
PreviewMouseLeftButtonDown="StackBlist_PreviewMouseLeftButtonDown"
PreviewMouseLeftButtonUp="StackBlist_PreviewMouseLeftButtonUp" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{x:Null}" IsEnabled="True" IsHitTestVisible="True" IsManipulationEnabled="False">
<!--PreviewMouseMove="StackBlist_PreviewMouseMove"-->
<Image x:Name="ImgOsCanvas" Source="/DrawTool;component/Images/铺展.png" Height=""></Image> <Button.Template>
<ControlTemplate TargetType="Button">
<Grid Background="#939393">
<Image x:Name="ImgOsCanvas" Source="/DrawTool;component/Images/铺展.png" Width="" Height=""></Image>
</Grid>
</ControlTemplate> </Button.Template> </Button>
<!--<TextBlock Height="" Margin="0,0,0,0" Background="#43464f">
</TextBlock>-->
<Menu x:Name="menuTools" ItemsSource="{Binding Source={StaticResource ResourceKey= toolsDS}}" Background="Transparent" ></Menu> </StackPanel> </Window>

二、获取元素的所有子元素

        /// <summary>
/// 获得指定元素的所有子元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public List<T> GetChildObjects<T>(DependencyObject obj) where T : FrameworkElement
{
DependencyObject child = null;
List<T> childList = new List<T>(); for (int i = ; i <= VisualTreeHelper.GetChildrenCount(obj) - ; i++)
{
child = VisualTreeHelper.GetChild(obj, i); if (child is T)
{
childList.Add((T)child);
}
childList.AddRange(GetChildObjects<T>(child));
}
return childList;
}

  如:获取Menu的所有MenuItem项

 List<System.Windows.Controls.MenuItem> mis = GetChildObjects<System.Windows.Controls.MenuItem>(this.menuTools);

三、更改菜单项内容

  1、简便的方式:

List<Image> images = GetChildObjects<Image>(mi);

  2、(比较笨的方法:按照ViasualTree图,一步步找的)

        /// <summary>
/// 设置菜单项的图标、标题
/// </summary>
/// <param name="mi">菜单项</param>
/// <param name="isConnected">是否已连接</param>
/// <param name="title"></param>
/// <param name="flag"></param>
/// <returns></returns>
private bool SetMenuContent(System.Windows.Controls.MenuItem mi,bool isConnected,string title = null,int flag = )
{
bool isSuccess = true;
try
{
XmlElement xe = mi.Header as XmlElement;
_selectedXelmt = xe;
string front = @"pack://application:,,,";
string menuItemTitle = xe.Attributes["Title"].Value;
string imgPath = front + xe.Attributes["ImagePath"].Value;
string imgPath0 = front + xe.Attributes["ImagePath0"].Value;
int index = ;
DependencyObject dpdcyObj = VisualTreeHelper.GetChild(mi, );
switch (flag)
{
case :
index = ;
break;
case :
index = ;
break;
default:
break;
}
DependencyObject dpdcyObj1 = VisualTreeHelper.GetChild(dpdcyObj, index); //menuItem 1, 子项是2
DependencyObject dpdcyObj2 = VisualTreeHelper.GetChild(dpdcyObj1, );
DependencyObject dpdcyObj3 = VisualTreeHelper.GetChild(dpdcyObj2, );
DependencyObject dpdcyObj4_1 = VisualTreeHelper.GetChild(dpdcyObj3, );
DependencyObject dpdcyObj4_2 = VisualTreeHelper.GetChild(dpdcyObj3, );
TextBlock tb = dpdcyObj4_2 as TextBlock;
if (isConnected && !string.IsNullOrEmpty(title))
{
tb.Text = title;
}
else
{
tb.Text = menuItemTitle;
}
//System.Windows.Forms.MessageBox.Show(tb.Text);
DependencyObject dpdcyObj5 = VisualTreeHelper.GetChild(dpdcyObj4_1, );
DependencyObject dpdcyObj6 = VisualTreeHelper.GetChild(dpdcyObj5, );
Image img = dpdcyObj6 as Image;
if (isConnected)
{
//string srcPath = img.Source.ToString().Replace("0", string.Empty);
//img.Source = new BitmapImage(new System.Uri(srcPath));
img.Source = new BitmapImage(new System.Uri(imgPath));
}
else
{
img.Source = new BitmapImage(new System.Uri(imgPath0));
}
//System.Windows.Forms.MessageBox.Show(img.ToString());
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
} return isSuccess;
}

WPF:获取控件内的子项的更多相关文章

  1. [WPF]获取控件间的相对位置

    原文:[WPF]获取控件间的相对位置 [WPF]获取控件间的相对位置                             周银辉 我们知道WPF有着比较灵活的布局方式,关于某个控件的坐标,Canv ...

  2. WPF获取控件的句柄

    在WinForm中,获得句柄是一件很容易的事情,This.Handle或者Control.Handle就可以,最近在WPF的开发中发现找不到这个属性,一番查找资料后找到了两种方式. 1,使用Windo ...

  3. WPF 获取控件模板中的控件

    DG是控件名称public T GetVisualChild<T>(DependencyObject parent, Func<T, bool> predicate) wher ...

  4. WPF获取控件内部的ScrollViewer,并控制ScrollViewer操作

    //获取内部  ScrollViewer方法 public static T FindVisualChild<T>(DependencyObject obj) where T : Depe ...

  5. WPF线程中获取控件的值和给控件赋值

    WPF中使用线程操作控件,按平常的操作方法操作的话会报异常:调用线程无法访问此对象,因为另一个线程拥有该对象.所以我们要使用Dispatcher类的BeginInvoke()与Invoke()方法.B ...

  6. WPF默认控件模板的获取和资源词典的使用

    一.获取默认的控件模板 WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例): 1.创建 ...

  7. WPF常用控件应用demo

    WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...

  8. asp.net js获取控件ID

    ClientID是由ASP.Net生成的服务器控件得客户端标识符,当这个控件生成到客户端页面的时候,在客户端代码访问该控件时就需要通过ClientID来访问. 以文本框为例: 一.未使用母版页 js可 ...

  9. WPF Image控件中的ImageSource与Bitmap的互相转换

    原文:WPF Image控件中的ImageSource与Bitmap的互相转换  1.从bitmap转换成ImageSource [DllImport("gdi32.dll", ...

随机推荐

  1. window操作技巧

    word中符号替换 1. 在网页复制内容时,下箭头用"^l"表示(字母L小写),回车用"^p"表示,可用替换命令全局替换(ctrl+f). 2. 添加底纹菜单在 ...

  2. RouterOS 软路由配置固定IP上网+DHCP

    实现要求: 局域网所有PC机自动获取IP地址,能相互访问并且能访问外网 环境要求: 一台PC机安装两张网卡 ( 使用常用的网卡芯片,例如Intel芯片.RTL瑞昱芯片等 ) 配置说明 1.外网IP地址 ...

  3. bootstrap实战经验

    凡是基本的布局需要float实现的,都可以考虑利用网格布局. 1,.jumbotron可以形成一个青灰色的背景,并自动调节对应边距 2,.panel的应用十分广泛,可以自动设置合适的padding.甚 ...

  4. 读取plist

    - (NSArray *)imageData { if (_imageData == nil) { // 从未初始化 // 初始化数据 // File : 全路径 // NSBundle : 一个NS ...

  5. nsstring打印结构体

    // //  main.m //  09-常用结构体 // //  Created by apple on 14-3-20. //  Copyright (c) 2014年 apple. All ri ...

  6. css 去除标签默认样式

    p,ul,ol,li,dl,dt,dd { list-style-type: none; margin: 0; padding: 0;}

  7. linux:/etc/rc.local 不能自动启动问题

    前段时间安装LNMP环境,配置/etc/rc.local的时候配置了启动mysql.nginx.php以及关闭防火墙,可结果重启了七八次还是自启动不了后来终于找到原因了 看下图: /etc/rc.lo ...

  8. Silverlight Popup Bubble

    控件下载地址: http://www.pudn.com/downloads217/sourcecode/others/detail1023372.html silverlight工程引入Liquid. ...

  9. jQuery 扩展功能

    源码如下: /*! * 说明:Jquery库扩展 * 创建时间: leo 2016/10/13 */ (function (window, jQuery, undefined) { jQuery.ex ...

  10. 在 Node.js 上调用 WCF Web 服务

    摘要:有时我们需要在WCF中做一些复杂数据处理和逻辑判断等,这时候就需要在NodeJS中调用WCF服务获取数据,这篇文件介绍如何在Node中调用WCF服务获取数据. Node项目中调用WCF服务获取数 ...