wpf中怎么查找控件啊

这句winfrom代码Controltitle=(Control)(Controls.Find("label_title_ct",true)[0]);wpf中怎么写啊... 这句winfrom 代码 Control title = (Control)(Controls.Find("label_title_ct", true)[0]);
wpf中怎么写啊
展开
 我来答
百度网友d242235
2015-01-23 · TA获得超过2385个赞
知道大有可为答主
回答量:1403
采纳率:84%
帮助的人:800万
展开全部

wpf由于大量使用模板,有时模板内的控件名称会不太方便查找,这时需要借助VisualTreeHelper和递归。

public static T FindChild<T>(DependencyObject parent, string childName)
   where T : DependencyObject
{    
  if (parent == null) return null;

  T foundChild = null;

  int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
  for (int i = 0; i < childrenCount; i++)
  {
    var child = VisualTreeHelper.GetChild(parent, i);
    // 如果子控件不是需查找的控件类型
    T childType = child as T;
    if (childType == null)
    {
      // 在下一级控件中递归查找
      foundChild = FindChild<T>(child, childName);

      // 找到控件就可以中断递归操作 
      if (foundChild != null) break;
    }
    else if (!string.IsNullOrEmpty(childName))
    {
      var frameworkElement = child as FrameworkElement;
      // 如果控件名称符合参数条件
      if (frameworkElement != null && frameworkElement.Name == childName)
      {
        foundChild = (T)child;
        break;
      }
    }
    else
    {
      // 查找到了控件
      foundChild = (T)child;
      break;
    }
  }

  return foundChild;
}

调用方法是这样的:

TextBox foundTextBox = 
   UIHelper.FindChild<TextBox>(Application.Current.MainWindow, "myTextBoxName");
百度网友f4e2724
2015-03-04 · TA获得超过298个赞
知道小有建树答主
回答量:372
采纳率:0%
帮助的人:168万
展开全部

简单好用,

 /// <summary>
        /// 获取子控件
        /// </summary>
        /// <typeparam name="T">子控件类型</typeparam>
        /// <param name="obj">父控件</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static T GetChildObject<T>(DependencyObject obj, string name) where T : FrameworkElement
        {
            DependencyObject child = null;
            T grandChild = null;
            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);


                if (child is T && (((T)child).Name == name && !string.IsNullOrEmpty(name)))
                {
                    return (T)child;
                }
                else
                {
                    grandChild = GetChildObject<T>(child, name);
                    if (grandChild != null)
                        return grandChild;
                }
            }
            return null;
        }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2015-02-04
展开全部
楼上的是可以查出的,不过如果在DataGrid模板列里面用这种方法好像就不行了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
FoolRabbit编程
2015-01-20 · 知道合伙人数码行家
FoolRabbit编程
知道合伙人数码行家
采纳数:619 获赞数:3805
毕业于苏州大学,4年编程经验,对WPF有研究。

向TA提问 私信TA
展开全部
FrameworkElement.

FindName 方法
查找具有提供的标识符名称(x:name)的元素。
追问
Label title = (Label)FrameworkElement.FindName("label_cszs_title" );
我写完后是错误的非静态方法或字段
追答
this.FindName

FrameworkElement指定父控件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式