在WPF中按名称获取UI元素(内部扩展器)

时间:2017-01-25 09:48:04

标签: c# wpf

由于某些原因,我需要使用动态方法生成我的UI。以下是我的代码片段。

        string xaml = "";
        UIElement elementM = null;
        xaml = xaml + "<StackPanel Orientation='Vertical'>";

        //Init & FreeSyn
        xaml = xaml + String.Format("<Expander Margin='{0} {1} 0 0' VerticalAlignment='Top'>", iLeftMostPadding, iTopPadding);
        xaml = xaml + "<Expander.Style>";
        xaml = xaml + "<Style TargetType='Expander'>";
        xaml = xaml + "<Setter Property='IsExpanded' Value='True' />";
        xaml = xaml + "<Setter Property='Header' Value='Axes'/>";
        xaml = xaml + "</Style>";
        xaml = xaml + "</Expander.Style>";
        xaml = xaml + "<Border BorderBrush='DodgerBlue' BorderThickness='1'  Margin='3 3 3 0'>";

        xaml = xaml + "<StackPanel Orientation='Vertical'>";
        xaml = xaml + "<StackPanel Orientation='Horizontal'>";
        xaml = xaml + String.Format("<Button Margin='{0} {1} 0 5' Name='btnCheckAll' Background='{2}' Foreground='{3}' Height='25' VerticalAlignment='Bottom' HorizontalAlignment='Right' Width='{4}'>Check All</Button>", iLeftPadding, iTopPadding, szBtnBG, szBtnFG, 82);
        xaml = xaml + String.Format("<Button Margin='{0} {1} 0 5' Name='btnUncheckAll' Background='{2}' Foreground='{3}' Height='25' VerticalAlignment='Bottom' HorizontalAlignment='Right' Width='{4}'>Uncheck All</Button>", iLeftPadding, iTopPadding, szBtnBG, szBtnFG, 82);
        xaml = xaml + "</StackPanel>";

        int nRow = nAxisCount / 4;
        if (nAxisCount % 4 != 0) { nRow++; }

        xaml = xaml + String.Format(" <UniformGrid Columns='4' Rows='{0}'>", nRow);
        for (int i = 0; i < nAxisCount; i++)
        {
            xaml = xaml + String.Format("<CheckBox Height='20' Name='chkIsAxis{0}'  Margin='{1} {2} 0 0' Width='40' IsChecked = 'true'>{0}</CheckBox>", i, 0, iTopPadding);
        }

        xaml = xaml + "</UniformGrid>";
        xaml = xaml + "</StackPanel>";
        xaml = xaml + "</Border>";
        xaml = xaml + "</Expander>";

elementM = (UIElement)XamlReader.Parse(xaml, context);
gridM.Children.Add(elementM);

string szTempM = "";

szTempM = "btnCheckAll";
Button btnCheckAll = CHelper.FindChild<Button>(elementM, szTempM); //Return Null Here !!!
btnCheckAll.Click += btnCheckAll_Click;

我没有按下“全部检查”按钮按名称“取消全部选中”。 如果在扩展器外部分配两个按钮,我只会成功。 你知道如何通过名字获得按钮吗? (扩展器内部)

我需要在其中添加一个onClick事件。

提前谢谢你。

UI看起来像这样:

enter image description here

以下是我的FindChild代码:

 public static T FindChild<T>(DependencyObject parent, string childName)
       where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        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);

            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's name is of the request name

                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }

        return foundChild;
    }

1 个答案:

答案 0 :(得分:1)

等到在您尝试获取对它们的引用之前,实际上已经创建了在XAML标记中定义的Button元素。你可以处理Loaded事件:

...
elementM = (UIElement)XamlReader.Parse(xaml);
gridM.Children.Add(elementM);

FrameworkElement fe = elementM as FrameworkElement;
if (fe != null)
{
    fe.Loaded += (s, e) => 
    {
        string szTempM = "btnCheckAll";
        Button btnCheckAll = CHelper.FindChild<Button>(fe, szTempM);
        btnCheckAll.Click += btnCheckAll_Click;
    };
}