如何将Mysql数据库与C#WPF中的Listbox中的Checkbox项绑定?

时间:2017-08-18 12:00:57

标签: c# mysql wpf checkbox binding

我在listbox中有一个DataTemplate个复选框。复选框项目表示用户的权限。当局位于数据库中的级别字段,仅包含数字变量(0到21)。但是在用户界面上,我们看到了当局的名字(21个权威机构名称)。我在CreateCheckBoxList方法的代码后面手工添加了名称。我想绑定数据库中的名称和值,并在Listbox中显示带有复选框的值,因为数据库中存在数值。我怎么能这样做?

这是数据库级别字段的屏幕截图: enter image description here

提前感谢您的回答..

XAML代码

  <StackPanel>
        <ListBox Name="lstUserDefination" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Multiple" Height="330">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBoxItem>
                        <CheckBox Name="chkUser" Content="{Binding AuthorityName}"  Tag="{Binding AuthorityValue}" IsChecked="{Binding IsChecked}"/>
                    </ListBoxItem>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

C#CODE

public partial class UserDefinationEdit : Window
{
    public ObservableCollection<Authority> authorityList { get; set; }

    public int sayi;
    public UserDefinationEdit()
    {
        InitializeComponent();
        CreateCheckBoxList();
        lstUserDefination.ItemsSource = authorityList;
    }

    public void CreateCheckBoxList()
    {
        authorityList = new ObservableCollection<Authority>();

        authorityList.Add(new Authority() {AuthorityValue = 0, AuthorityName = "0 - ********" });
        authorityList.Add(new Authority() { AuthorityValue = 1, AuthorityName = "1 - *********" });
        authorityList.Add(new Authority() { AuthorityValue = 2, AuthorityName = "2 - *********" });
        authorityList.Add(new Authority() { AuthorityValue = 3, AuthorityName = "3 - *******" });
        authorityList.Add(new Authority() { AuthorityValue = 4, AuthorityName = "4 - *******" });
        authorityList.Add(new Authority() { AuthorityValue = 5, AuthorityName = "5 - *********" });
        authorityList.Add(new Authority() { AuthorityValue = 6, AuthorityName = "6 - ********" });
        .
        .
        .
        .

        this.DataContext = this;
    }

    private void btnUnselectall_Click(object sender, RoutedEventArgs e)
    {
        foreach (var a in authorityList)
        {
            a.IsChecked = false;
        }
    }

    private void btnSelectAll_Click(object sender, RoutedEventArgs e)
    {
        foreach (var a in authorityList)
        {
            a.IsChecked = true;
        }
    }
}

public class Authority : INotifyPropertyChanged
{
    private string authorityName;
    private int authorityValue;
    private bool isChecked;

    public string AuthorityName
    {
        get { return authorityName; }
        set
        {
            authorityName = value;
            NotifyPropertyChanged();
        }
    }

    public int AuthorityValue
    {
        get { return authorityValue; }
        set
        {
            authorityValue = value;
            NotifyPropertyChanged();
        }
    }

    public bool IsChecked
    {
        get { return isChecked; }
        set
        {
            isChecked = value;
            NotifyPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

}

0 个答案:

没有答案