radiobutton如果固定的写在Xaml文件里,遇到改动的话需要修改程序,优克斯股票软件里的radiobutton统一放到了Python脚本文件里调用set,当需要修改的时候直接操作py脚本文件 以下是我做的一个小demo,直接在程序中调用set方法
public MainWindow(){InitializeComponent();set("te", "aaa");set("te", "bbb");set("te", "ccc");set("te", "ddd");set("te", "eee");var list = configs["te"];Radiobutton rb=new Radiobutton("te");foreach (var i in list){rb.addButton(i);}this.xxx.Children.Add(rb);}Dictionary<string, List<object>> configs = new Dictionary<string, List<object>>();public void set(string label, string value){if (configs.ContainsKey(label)){configs[label].Add(value);return;}var list = new List<object>();configs.Add(label, list);list.Add(value);}
class Radiobutton:Grid{List<RadioButton> lrb = new List<RadioButton>();string name = null;public Radiobutton(string name){this.name = name;}public void addButton(object content){this.ColumnDefinitions.Add(new ColumnDefinition()//增加一列{Width = new System.Windows.GridLength(58),});var b = new RadioButton(){Content = content,GroupName = name,};//b.Checked += new System.Windows.RoutedEventHandler(b_Checked);Grid.SetColumn(b, lrb.Count);lrb.Add(b);if (lrb.Count == 1){b.IsChecked = true;}this.Children.Add(b);}}