我尝试在我的wpf中实现CheckedListbox。我想从列表中填充它,然后使用复选框
选择列表项 <ListBox Grid.Row="3" Grid.Column="0" ItemsSource="{Binding selectedList}" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding CheckedList.SetText}" IsChecked="{Binding CheckedList.Checked, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是我的VM类
public class CheckedList : INotifyPropertyChanged
{private string _text;
public string SetText
{
get { return _text; }
set { _text = value;
OnPropertyChanged("SetText");
}
}
private bool _checked;
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
OnPropertyChanged("Checked");
}
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
class MigratorAdvancedMigrationOptionsViewModel : INotifyPropertyChanged
{
Migrator _Migrator { get; set; }
CloseClass _Close { get; set; }
public RelayCommand<IClosable> CloseWindowCommand { get; private set; }
public RelayCommand<IShowable> ShowWindowCommand { get; private set; }
public RelayCommand<IShowable> BackWindowCommand { get; private set; }
public string WindowTitle { get; private set; }
Window _window;
private RelayCommand<object> _setRequirements;
public ObservableCollection<string> List { get; set; }
public ObservableCollection<CheckedList> selectedList { get; set; }
public MigratorAdvancedMigrationOptionsViewModel(Migrator _migrator, CloseClass _Close)
{
this.ShowWindowCommand = new RelayCommand<IShowable>(this.ShowWindow);
this.BackWindowCommand = new RelayCommand<IShowable>(this.Back);
this._setRequirements = new RelayCommand<object>(Set_Requirements);
_onClosingCommand = new RelayCommand<object>(OnClosing_Command);
_Migrator = _migrator;
this._Close = _Close;
List = (ObservableCollection<string>)_migrator.GetTables();
selectedList = new ObservableCollection<CheckedList>();
CheckedList checkedList= new CheckedList();
foreach (string item in List)
{
checkedList.SetText = item;
checkedList.Checked = false;
selectedList.Add(checkedList);
}
}
private RelayCommand<object> _onClosingCommand;
public ICommand OnClosingCommand
{
get { return _onClosingCommand; }
}
private void OnClosing_Command(object obj)
{
_Close.IsClosedFromButton = true;
_Close.CancelConfirmation(obj);
}
public ICommand SetRequirements
{
get { return _setRequirements; }
}
private void Set_Requirements(object obj)
{
_window = System.Windows.Application.Current.Windows[4];
_Close.Set_Requirements(_window);
}
private void ShowWindow(IShowable window)
{
}
private void Back(object obj)
{
_Close.Back(3, 4);
_Migrator._windowState = SpaixDM.NetDesktop.Models.WindowState.options;
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
通过调试我只看到一个带有5个复选框的文本框(selectedList中有5个项目),仅此而已。 我也尝试了这种变化
public class CheckedList
{
public string Text { get; set; }
public bool Value { get; set; }
}
class MigratorAdvancedMigrationOptionsViewModel : INotifyPropertyChanged
{
Migrator _Migrator { get; set; }
CloseClass _Close { get; set; }
public RelayCommand<IClosable> CloseWindowCommand { get; private set; }
public RelayCommand<IShowable> ShowWindowCommand { get; private set; }
public RelayCommand<IShowable> BackWindowCommand { get; private set; }
public string WindowTitle { get; private set; }
Window _window;
private RelayCommand<object> _setRequirements;
public ObservableCollection<string> List { get; set; }
public ObservableCollection<CheckedList> selectedList { get; set; }
public MigratorAdvancedMigrationOptionsViewModel(Migrator _migrator, CloseClass _Close)
{
this.ShowWindowCommand = new RelayCommand<IShowable>(this.ShowWindow);
this.BackWindowCommand = new RelayCommand<IShowable>(this.Back);
this._setRequirements = new RelayCommand<object>(Set_Requirements);
_onClosingCommand = new RelayCommand<object>(OnClosing_Command);
_Migrator = _migrator;
this._Close = _Close;
List = (ObservableCollection<string>)_migrator.GetTables();
selectedList = new ObservableCollection<CheckedList>();
CheckedList checkedList= new CheckedList();
foreach (string item in List)
{
checkedList.Text = item;
SetText = item;
checkedList.Value = false;
Checked = false;
selectedList.Add(checkedList);
}
}
private string _text;
public string SetText
{
get { return _text; }
set { _text = value;
OnPropertyChanged("SetText");
}
}
private bool _checked;
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
OnPropertyChanged("Checked");
}
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
它也不起作用
答案 0 :(得分:0)
您将ListBox
绑定到selectedList
,ObservableCollection
CheckedList
。您应该将CheckBox
的内容绑定到CheckedList.SetText,而应该只绑定到SetText。
<DataTemplate>
<CheckBox Content="{Binding SetText}" IsChecked="{Binding Value, Mode=TwoWay}"/>
</DataTemplate>
数据模板定义了列表中每个对象的显示内容,数据上下文将设置为该对象,即CheckedList实例。通过放置CheckedList.SetText,绑定在CheckedList类中查找不存在的CheckedList属性。