I have a listbox contains checkboxes in DataTemplate.The checkbox items symbolize the authorities of the users.And the authorities are located in database with only numeric variables(0 to 6).But on the UI,we see the names of the authorities(6 authority names).I added the names by hand behind the code at CreateCheckBoxList method.I want to bind the names and values in the database and display the values with checkboxes in Listbox due to the numeric values at the database.How can i do that?
Thanks for your answers in advance..
XAML CODE
<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));
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire