首先,我有一个包含生成的radiobuttons的列表。这看起来像这样:
foreach (Team TeamName in loadOperation.Entities)
{
RadioButton radiobutton = new RadioButton();
radiobutton.GroupName = "ticketfilter";
radiobutton.Content = TeamName.Name.ToString();
int teamid = TeamName.TeamID;
radiobutton.Checked += new RoutedEventHandler((s, e) => FilterForTeam(s, e, teamid));
radiobutton.Click += new RoutedEventHandler((s, e) => LoadTickets());
UserRadioButtons.Children.Add(radiobutton);
}
结果,这给了我一个RadioButtons列表。但是,我想将此列表转换为树视图 所以我做了以下事情:
foreach (Team TeamName in loadOperation.Entities)
{
TreeViewItem tree = new TreeViewItem();
tree.Header = new RadioButton()
{
GroupName = "ticketfilter",
Content = TeamName.Name.ToString(),
};
int count = TeamName.TeamMemberships.Select(d => d.User.UserID).Count();
int startcount = 0;
while (startcount < count)
{
tree.Items.Add(new TreeViewItem()
{
Header = new RadioButton()
{
GroupName = "ticketfilter",
Content = TeamName.TeamMemberships.Select(d => d.User.FullName).Skip(startcount).FirstOrDefault(),
//checked = something // This turns into .Ischecked,
}
});
startcount++;
}
UserRadioButtons.Children.Add(tree);
}
现在这给了我想要的结果。
但是,我无法将属性已选中添加到我的RadioButton。
有没有办法可以将 .Checked 和 .Click 等属性分配到树形视图中的RadioButtons?
编辑:
我现在在做什么的例子。
public class MyRadioButtonItem
{
public RadioButton MyRadioButton { get; set; }
public bool IsChecked { get; set; }
public string content { get; set; }
public string Groupname { get; set; }
}
然后我将它定义为:
TreeViewItem tree = new TreeViewItem();
tree.Header = new MyRadioButtonItem()
{
Groupname = "ticketfilter",
content = TeamName.Name.ToString(),
};
它承认我正在定义的所有事情。 但是,我需要选项:点击并选中。
但是例如在这个例子中。 radiobutton如何知道GroupName和Content之间的区别。 例如,Groupname可以是内容,因为它们都是字符串?
答案 0 :(得分:0)
是的,您可以创建一个这样的自定义类:
public class MyRadioButtonItem
{
public RadioButton MyRadioButton {get; set;}
public bool IsChecked {get; set;}
}
然后用它代替RadioButton
如果有帮助,请告诉我。