事先道歉,因为我知道这个问题多次出现过.但是,我正在努力找出我自己的代码出错的地方.只需查找旁边的复选框和名称列表.目前它编译好,但ListBox为空.
所有代码都在一个名为ucDatabases的控件中.
XAML:
<ListBox Grid.Row="4" ItemsSource="{Binding Databases}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" Margin="5 5 0 0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
C#代码:
public ObservableCollection<CheckBoxDatabase> Databases;
public class CheckBoxDatabase : INotifyPropertyChanged
{
private string name;
private bool isChecked;
public Database Database;
public bool IsChecked
{
get { return isChecked; }
set
{
isChecked = value;
NotifyPropertyChanged("IsChecked");
}
}
public string Name
{
get { return name; }
set
{
name = value;
NotifyPropertyChanged("Name");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string strPropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
帮助方法填充一些测试数据:
private void SetTestData()
{
const string dbAlias = "Database ";
Databases = new ObservableCollection<CheckBoxDatabase>();
for (int i = 0; i <= 4; i++)
{
var db = new Database(string.Format(dbAlias + "{0}", i));
var newCBDB = new CheckBoxDatabase {Database = db, IsChecked = false, Name = db.Name};
Databases.Add(newCBDB);
}
}
Run Code Online (Sandbox Code Playgroud)
建议和解决方案将不胜感激!
public ObservableCollection<CheckBoxDatabase> Databases;
是一个领域.
您应该用属性替换它:
public ObservableCollection<CheckBoxDatabase> Databases {get;set;};
别忘了INotifyPropertyChanged
!
归档时间: |
|
查看次数: |
16405 次 |
最近记录: |