如何从ComboBox控件获取ToggleButton [英] How do I get the ToggleButton from ComboBox Control

查看:332
本文介绍了如何从ComboBox控件获取ToggleButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查ComboBox的控制模板( http ://msdn.microsoft.com/en-us/library/ms752094(v = vs.110).aspx ),在那里他们已经使用切换按钮来切换弹出窗口。有没有办法我可以从代码后面切换按钮?



我试过这个,但没有效果: - (

  var uiElement =(ComboBox)sender; 
var toggleButton = uiElement.FindResource(typeof(ToggleButton))as ToggleButton;


解决方案

如果你现在是ToggleButton的名字,那么你可以使用下面的代码:

  var uiElement =(ComboBox)sender; 
var toggleButton = uiElement.Template.FindName(< Your ToggleButton Name Here> ,uiElement)as ToggleButton;

if(toggleButton!= null)
{
//你的代码在这里
}

如果你不知道ToggleButton的名字,那么在这种情况下,唯一的选择是遍历视觉树,类型切换按钮示例代码如下:

 内部静态列表< T> FindVisualChild< T>(此DependencyObject depObj) DependencyObject 
{
if(depObj!= null)
{
List< T> childItems = null;
for(int i = 0; i {

if(childItems == null)
childItems = new List< T>();

DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
if(child!= null&& child is T)
{
childItems.Add((T)child);
}

var recursiveChildItems = FindVisualChild< T>(child);
if(recursiveChildItems!= null&& recursiveChildItems.Count> 0)
childItems.AddRange(recursiveChildItems);
}
return childItems;
}
return null;
}

上面的方法是DependencyObject的扩展,从视觉树中键入。如果你只想指定类型的第一个元素,那么你可以稍微改变方法,并且可以打破循环,当你获得指定类型的第一个元素并返回它。


I was checking control template of ComboBox(http://msdn.microsoft.com/en-us/library/ms752094(v=vs.110).aspx), where they have used Toggle Button to toggle the Popup. Is there a way I could get the toggle button from code behind?

I had tried this, but to no avail :-(

var uiElement = (ComboBox)sender;
var toggleButton = uiElement.FindResource(typeof(ToggleButton)) as ToggleButton;

解决方案

If you now the name of the ToggleButton then you can use following code:

var uiElement = (ComboBox)sender;
var toggleButton = uiElement.Template.FindName("<Your ToggleButton Name Here>",uiElement) as ToggleButton;

if(toggleButton!=null)
{
     // Your code goes here.
}

If you don't know the name of the ToggleButton then in that case the only option is to travers through visual tree and find an element of type toggle button. Sample code below:

    internal static List<T> FindVisualChild<T>(this DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            List<T> childItems = null;
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {

                if (childItems == null)
                    childItems = new List<T>();

                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    childItems.Add((T)child);
                }

                var recursiveChildItems = FindVisualChild<T>(child);
                if (recursiveChildItems != null && recursiveChildItems.Count > 0)
                    childItems.AddRange(recursiveChildItems);
            }
            return childItems;
        }
        return null;
    }

Above method is an extension to DependencyObject and will return all the elements of the specified type from the visual tree. If you want only first element of specified type then you can make slight changes in the method and can break the loop when you get the first element of the specified type and return it.

这篇关于如何从ComboBox控件获取ToggleButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆