티스토리 뷰
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
'WPF' 카테고리의 다른 글
Mysql Aes_Decrypt 쿼리 == C# Decrypt String(HEX) (0) | 2021.06.14 |
---|---|
DataTable Select 구문 오류: 'B' 연산자 뒤에 피연산자가 없습니다. (0) | 2021.04.29 |
WPF Scrollviewer in ScrollViwer header Grid freezing (0) | 2021.04.26 |
DateTime Nullable Value (0) | 2021.04.21 |
WPF StackPanel(Back Layer) in Button Click Enable (0) | 2021.03.04 |
- Total
- Today
- Yesterday
- C# LINQ Left join
- FileStream Add Byte
- Entry '' has empty native path
- Xamarin.Ios Firebase Phone Auth
- Label Text LineBreak in Xaml
- ssl_client_socket_impl.cc
- Linux SSH Multi Computer Join
- Xamarin.Ios Firebase Phone SMS OTP Send
- WPF Datagrid Cell Value Change
- SkiaSharp
- Embeded 한글Font적용
- Xamarin Firebase Phone Auth
- 연산자 뒤에 피연산자가 없습니다.
- Xamarin.Forms
- 암호 마스터키
- Xamarin SMS OTP Send
- ClickOnce 인증서 인증기간 변경
- WPF Textbox
- Microcharts
- Xamarin Firebase Phone User Add
- WPF Scrollviewer in ScrollViwer
- c# Encrypt / Decrypt
- 서버 수준의 URN 필터
- Windows IIS FTP 디렉토리 목록 오류
- WPF Excel Export Microsoft.Office.Interop 성능향상(열 기준으로 복사)
- GetCellContent CheckBox Value
- Xamarin reCAPTCHA
- Xamarin.Ios Firebase Phone User Add
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |