wpfのラップパネル内でチェックボックスを動的に作成しました。コード内のチェックボックスをどのように見つけるか。あなたはより多くの再利用可能なコンパクトではないと特殊なバージョンが必要な場合wpfでチェックされた動的に作成されたCheckBoxを見つける方法
2
A
答えて
2
..
WrapPanel wp = new WrapPanel();
for (int i = 0; i < 10; i++)
{
CheckBox chb = new CheckBox();
chb.Name = string.Format("Id{0}", i);
wp.Children.Add(chb);
}
foreach (CheckBox el in wp.Children)
{
if (el.Name == "Id3")
{
return el;
}
}
1
/// <summary>
/// Finds a Child of a given item in the visual tree.
/// </summary>
/// <param name="parent">A direct parent of the queried item.</param>
/// <typeparam name="T">The type of the queried item.</typeparam>
/// <param name="childName">x:Name or Name of child. </param>
/// <returns>The first parent item that matches the submitted type parameter.
/// If not matching item can be found,
/// a null parent 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;
}
1
あなたが選択したチェックボックスのテキストを探している場合は、thesというように気にいらない操作を行います。
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
string a =checkBox1.Text;
}
関連する問題
- 1. CheckBoxを動的に作成する動的に作成されたGridView
- 2. aspxページのチェックされたラジオボタンを見つける方法
- 3. 動的にロードされたモジュールを静的に見つける方法
- 4. divで動的に作成されたコントロール(TextBox)を見つけよう
- 5. WPF:動的に作成されたWPFウィンドウでコントロールを動的に追加する方法
- 6. jQueryチェックボックスは、チェックされたプロパティで動的に作成されました
- 7. チェックボックスでチェックされた入力の長さを見つける方法は?
- 8. WPFで動的に作成されたjpegをデータバインド
- 9. WPF - メニューハンドラメソッドでクリックされたアイテムを見つけるには?
- 10. 動的に追加されたテキストボックスの値を見つける
- 11. WPFで名前を使用してcodebehindで作成されたコントロールを見つける方法
- 12. WPFから動的に作成されたRadioButtonをコード
- 13. SpringBootアプリケーションで作成されたJDBC接続プールスレッドを見つける方法は?
- 14. ClearCaseで新しく作成されたファイルを見つける方法
- 15. 動的に作成されたdivをカウントする方法
- 16. 動的に生成されたフォームの送信時に、どのサブミットボタンがクリックされたかを見つける
- 17. 別のイベントハンドラメソッドで動的に作成されたCheckBoxイベント状態にアクセスする方法
- 18. はJQuery:動的に生成されたHTMLの要素をスニペット見つける
- 19. jQueryがダイナミックに作成されたテーブル/セルのcheckBox値を見つけられない
- 20. div、label、checkboxのような動的に作成されたhtmlgenericcontrolsには、C#
- 21. CodeNameOne動的に作成されたフォーム、「戻る」方法
- 22. 動的に生成されたCheckBox値を別のアクティビティに渡すと、Nullだけが返されますか?
- 23. 指定された方法で配列を動的に作成する方法
- 24. 以前にチェックされたアイテムを自動的にチェックする方法は?
- 25. 動的に作成されたボタン
- 26. は、動的に作成されたHTML
- 27. mongoDB動的に作成されたフィールド
- 28. 動的に作成されたテーブルクエリ
- 29. ASP.NET - 動的に作成されたボタン
- 30. Reactjs:動的に作成されたリストコンポーネント
缶関連コードを追加してください –
チェックボックスに名前を付けると、簡単に見つけることができるはずです – BugFinder