簡體   English   中英

在 WPF 中按名稱獲取 UI 元素(擴展器內部)

[英]Get UI Element (Inside Expander) by name in 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 事件。

先感謝您。

用戶界面如下所示:

在此處輸入圖片說明

以下是我的 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;
    }

等到您在 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;
    };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM