動的に ListBox を下記のコードで横にいくつか並べるように作製しております。
それ自身問題なくできておりますが、作成・表示後に再度その listBox を参照したいことが発生しました。
listBox 内のデータをバーコードリーダーで読み込ませた番号と一致する番号、例えば図の F2221346 番号の行の背景色を変更したい。
C#
1 private void AddListBox(List<string> list) 2 { 3 // ListBoxを動的作成追加(https://gogowaten.hatenablog.com/entry/2020/03/17/133941)参照 4 var listBox = new ListBox(); 5 6 // ListBoxのItemsSourceのBindingはソースの指定もない空のBinding 7 listBox.SetBinding(ListBox.ItemsSourceProperty, new Binding()); 8 listBox.SetValue(TextBlock.MarginProperty, new Thickness(2, 5, 0, 5)); 9 10 // ListBoxのアイテムテンプレート作成、設定 11 var panel = new FrameworkElementFactory(typeof(StackPanel)); 12 panel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); 13 //panel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); 14 // 番号 15 var textBlock = new FrameworkElementFactory(typeof(TextBlock)); 16 textBlock.SetBinding(TextBlock.TextProperty, new Binding(nameof(DoctorList.Hyohon))); 17 textBlock.SetValue(TextBlock.MarginProperty, new Thickness(5, 1, 5, 1)); 18 textBlock.SetValue(FrameworkElement.WidthProperty, (double)50); 19 /* textBlock.SetValue(FrameworkElement.HeightProperty, (double)15); */ 20 panel.AppendChild(textBlock); 21 // 名前 22 var textBlock2 = new FrameworkElementFactory(typeof(TextBlock)); 23 textBlock2.SetBinding(TextBlock.TextProperty, new Binding(nameof(DoctorList.KanjaMei))); 24 textBlock2.SetValue(TextBlock.MarginProperty, new Thickness(0, 1, 5, 1)); 25 textBlock2.SetValue(FrameworkElement.WidthProperty, (double)80); 26 panel.AppendChild(textBlock2); 27 // チェック 28 var check = new FrameworkElementFactory(typeof(CheckBox)); 29 check.SetBinding(CheckBox.IsCheckedProperty, new Binding(nameof(DoctorList.scCheck))); 30 check.SetValue(TextBlock.MarginProperty, new Thickness(0, 1, 2, 1)); 31 panel.AppendChild(check); 32 // ギムザ 33 var check2 = new FrameworkElementFactory(typeof(CheckBox)); 34 check2.SetBinding(CheckBox.IsCheckedProperty, new Binding(nameof(DoctorList.gCheck))); 35 check2.SetValue(TextBlock.MarginProperty, new Thickness(0, 1, 2, 1)); 36 panel.AppendChild(check2); 37 38 var dt = new DataTemplate(); 39 dt.VisualTree = panel; 40 listBox.ItemTemplate = dt; 41 42 //追加(表示) 43 panel2.Children.Add(listBox); 44 45 46 // タイトルをつけるために下記を作成 47 // 48 var Box = new TextBox(); 49 Box.SetBinding(ListBox.ItemsSourceProperty, new Binding()); 50 Box.SetValue(TextBlock.MarginProperty, new Thickness(2, 5, 0, 5)); 51 var title = new FrameworkElementFactory(typeof(StackPanel)); 52 title.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal); 53 var text = new FrameworkElementFactory(typeof(TextBlock)); 54 text.SetBinding(TextBlock.TextProperty, new Binding(nameof(DoctorList.Dr))); 55 text.SetValue(TextBlock.MarginProperty, new Thickness(20, 1, 5, 1)); 56 text.SetValue(FrameworkElement.WidthProperty, (double)165); 57 text.SetValue(FrameworkElement.HeightProperty, (double)20); 58 text.SetValue(TextBlock.FontSizeProperty, 18.0); 59 title.AppendChild(text); 60 61 62 FrameworkElementFactory stackPanelFactory = new FrameworkElementFactory(typeof(StackPanel)); 63 stackPanelFactory.AppendChild(title); 64 // CellTemplate を作成 65 ControlTemplate template = new ControlTemplate(); 66 // 作成したビジュアルツリーをセット 67 template.VisualTree = stackPanelFactory; 68 Box.Template = template; 69 70 //追加(表示) 71 panel1.Children.Add(Box); 72 73 74 //表示するデータ作成、設定 75 listBox.DataContext = MakeList(list); 76 Box.DataContext = MakeList(list); 77 } 78 79 Model._custmer に listBox を表示するデータが入っています。番号でそのデータを抽出します。 80 81 private System.Collections.ObjectModel.ObservableCollection<DoctorList> MakeList(List<string> lst) 82 { 83 var list = new System.Collections.ObjectModel.ObservableCollection<DoctorList>(); 84 for (int i = 0; i < lst.Count; i++) 85 { 86 var custum = Model._custmer.Where(x => x.Hyohon == lst[i]).FirstOrDefault(); 87 88 list.Add(new DoctorList(custum.No, custum.Hyohon, custum.IinCode, custum.KanjaMei, custum.Bui, custum.Check, custum.Dr, 89 custum.scCheck, custum.gCheck, Model._custmer[i].ForceSet)); 90 } 91 92 return list; 93 }
xaml
1 <ScrollViewer HorizontalScrollBarVisibility="Auto" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" Margin="5"> 2 <StackPanel Orientation="Vertical"> 3 <DockPanel x:Name="panel1" /> 4 <StackPanel Orientation="Horizontal"> 5 <DockPanel x:Name="panel2" /> 6 </StackPanel> 7 </StackPanel> 8 </ScrollViewer>
コントロールの一覧を表示しよう (VisualTreeHelperとLogicalTreeHelperの違い) その①より
https://qiita.com/furugen/items/185c1815625e2a58d0a8
下記でコントロールが多数出てきますが、ここからどうやって目的のコントロールを見つけるのでしょうか?
C#
1 private void ShowVisualControl(DependencyObject tgt, int position = 0) 2 { 3 for (int i = 0; i < VisualTreeHelper.GetChildrenCount(tgt); i++) 4 { 5 DependencyObject childObj = VisualTreeHelper.GetChild(tgt, i); 6 // ログ出力 7 ShowLog(childObj, position); 8 list.Add(childObj.ToString()); 9 10 if (VisualTreeHelper.GetChildrenCount(childObj) > 0) 11 { 12 int nextPosition = position + 1; 13 ShowVisualControl(childObj, nextPosition); 14 } 15 } 16 } 17 private static void ShowLog(DependencyObject tgt, int position = 0) 18 { 19 string tab = null; 20 string baseInfo = tgt.ToString(); 21 22 for (int i = 0; i < position; i++) 23 { 24 tab += "\t"; 25 } 26 27 // 下記情報を出力 28 // 基本情報 : コンテンツ 29 System.Diagnostics.Debug.WriteLine(tab + baseInfo); 30 }
Windows11 VS2019 WPF C#
【修正】
自己レスです。
まだ、答え近づいているのかわかりませんが、
ListBox lbl = (ListBox)panel2.Children.OfType<FrameworkElement>().FirstOrDefault(a => a.Name == "");
var lb = panel2.Children.OfType<FrameworkElement>();
でも、 ListBox が探せました。
回答2件
あなたの回答
tips
プレビュー