简体   繁体   English

WPF XAML数据库驱动的复选框值

[英]WPF XAML database driven checkbox values

how do I display an unknown number of checkboxes in XAML? 如何在XAML中显示未知数量的复选框? I'm querying the database to extract a list of possible values, and then I want to display those values as a list of checkboxes. 我正在查询数据库以提取可能值的列表,然后我想将这些值显示为复选框列表。 Some DB tables may have a small number of values, others could potentially have 100+. 一些数据库表可能包含少量值,另一些数据库表可能包含100+个值。

XAML: XAML:

<ListBox x:Name="SuppliersSearchStatusListBox" Margin="100,100,0,0" ScrollViewer.CanContentScroll="True" AllowDrop="True" MaxHeight="50" MaxWidth="200" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding ListStatusOptions}">
  <CheckBox Content="{Binding arrStatusOption}"/>
</ListBox>

CS: CS:

public void ListStatusOptions()
{
    // Query, vars and debug info removed
    conn.Open();
    SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQLQuery, conn);
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "StatusOptions");

    int varArrCount = ds.Tables[0].Rows.Count;

    string[] arrStatusOption = new string[varArrCount];
    int i = 0;
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        arrStatusOption[i] = dr["StatusValue"].ToString();
        i++;
    }
    conn.Close();
}

The DB query returns results into the array and the values are as expected, but a list of checkboxes do not appear on the XAML page. 数据库查询将结果返回到数组中,并且值与预期的一样,但是XAML页上没有出现复选框列表。 What am I missing? 我想念什么?

Use data templates to template each item in the ListBox: 使用数据模板来模板化ListBox中的每个项目:

<ListBox x:Name="SuppliersSearchStatusListBox"
         scrollViewer.CanContentScroll="True"
         AllowDrop="True"
         ItemsSource="{Binding ListStatusOptions}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding arrStatusOption}"
                      IsChecked="{Binding IsChecked}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Then change ListStatusOptions to a new class with an IsSelected property to bind to instead of just strings 然后将ListStatusOptions更改为具有IsSelected属性绑定的新类,而不仅仅是字符串

public class StatusOption
{
    public string name { get; set; }
    public bool IsSelected { get; set; }
}

Then change your code to use this new class 然后更改您的代码以使用此新类

public void ListStatusOptions()
{
    // Query, vars and debug info removed
    conn.Open();
    SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQLQuery, conn);
    DataSet ds = new DataSet();
    dataAdapter.Fill(ds, "StatusOptions");

    int varArrCount = ds.Tables[0].Rows.Count;

    var arrStatusOption = new StatusOption[varArrCount];
    int i = 0;
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        arrStatusOption[i] = new StatusOption
                                 {
                                     Name = dr["StatusValue"].ToString(),
                                     IsSelected = false
                                 }
        i++;
    }
    conn.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM