列表框中带有单选按钮和复选框

时间:2015-03-02 10:18:31

标签: c# listbox

我需要一个列表框,其中包含前4个项目的复选框和下一个4项目的单选按钮....这些项目是从数据库驱动....我可以从复选框中选择任意数量的项目一个来自单选按钮。如何从数据库动态添加到列表框

 listBox1.Items.Add("Sunday"); 
listBox1.Items.Add("Monday"); 
listBox1.Items.Add("Tuesday"); 
listBox1.Items.Add("Wednesday"); 
listBox1.Items.Add("Thursday"); 
listBox1.Items.Add("Friday"); 
listBox1.Items.Add("Saturday");

1 个答案:

答案 0 :(得分:1)

你可以这样:

<强>的Xaml:

        <ListBox Name="uiList" Width="300" Height="600"/>

<强>代码:

 for (int i = 0; i < Enum.GetValues(typeof(DayOfWeek)).Length; i++)
        {
            StackPanel panel = new StackPanel() { Orientation = Orientation.Horizontal };

            if (i < 4)
            {
                panel.Children.Add(new CheckBox());
            }
            else
            {
                panel.Children.Add(new RadioButton() { GroupName = "SameGRoupName" });
            }
            panel.Children.Add(new TextBlock() { Text = ((DayOfWeek)i).ToString() });
            uiList.Items.Add(panel);
        }

<强>结果:

enter image description here

Demo project