티스토리 뷰

728x90
반응형

Label을 Dynamic하게 생성하고 생성된 Label을 찾을때 FindName으로 찾으면 Null이다.

// Label 생성
LblV = new Label()
{ Name = "K1"
Content = NDT.Rows[i].ItemArray[k].ToString(),
Width = _Width,
Height = 25,
Background = new SolidColorBrush(Color.FromArgb(30, 20, 91, 108)),
Foreground = new SolidColorBrush(System.Windows.Media.Colors.Black),
HorizontalContentAlignment = HorizontalAlignment.Center,
Margin = new Thickness(0.5, 0.5, 0.5, 0.5),
};
LblV.MouseMove += new MouseEventHandler(Mouse_move);
LblV.MouseLeave += new MouseEventHandler(Mouse_leave);
LblV.MouseDown += new MouseButtonEventHandler(Mouse_Down);

// Label 찾기
var LBL = (Label)FindName("K1"); // is Null Value


  Label LBL = new Label();
        for (int i =0; i < 6; i++)
        {

            LBL = UIHelper.FindChild<Label>(this, "K" + i.ToString());
            if (LBL is null)
            {
                Console.WriteLine("NULL:"+i.ToString());
            }
            else
            {
                LBL.Background = new SolidColorBrush(System.Windows.Media.Colors.HotPink);
            }
        }


   ---------------------------------------     

    public static class UIHelper
    {
        /// <summary>
        /// Finds a parent of a given item on the visual tree.
        /// </summary>
        /// <typeparam name="T">The type of the queried item.</typeparam>
        /// <param name="child">A direct or indirect child of the queried item.</param>
        /// <returns>The first parent item that matches the submitted type parameter. 
        /// If not matching item can be found, a null reference is being returned.</returns>
        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;
        }
    }

출처: https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type

반응형
댓글