在列表框内绑定 MVVM CheckBox
Binding MVVM CheckBox inside a Listbox
我正在尝试在我的应用程序中使用模式 MVVM Windows Phone。但是我在绑定列表框内的 CheckBox 时遇到了问题。
这是我的。xaml
<ListBox x:Name="LstbTagsFavoris" SelectionChanged="favoris_SelectionChanged" Margin="10,10,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Foreground="#555" Background="Red" Loaded="CheckBox_Loaded" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" Content="{Binding Categories}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的视图模型
public class CategorieViewModel
{
private List<string> _Categories = new List<string>();
public List<string> Categories
{
get
{
return _Categories;
}
set
{
_Categories = value;
}
}
public void GetCategories()
{
Categories = GlobalVar._GlobalItem.SelectMany(a => a.tags)
.OrderBy(t => t)
.Distinct()
.ToList();
}
在我的 xaml.cs 中:
CategorieViewModel c = new CategorieViewModel();
c.GetCategories();
this.DataContext = c;
但是没用
实现 INotifyPropertyChanged 接口。
这样做。
public class CategorieViewModel : INotifyPropertyChanged
{
private List<string> _Categories = new List<string>();
public List<string> Categories
{
get
{
return _Categories;
}
set
{
_Categories = value;
OnPropertyChanged("Categories");
}
}
public void GetCategories()
{
Categories = GlobalVar._GlobalItem.SelectMany(a => a.tags)
.OrderBy(t => t)
.Distinct()
.ToList();
}
protected void OnPropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
在XAML代码中:
<ListBox x:Name="LstbTagsFavoris" ItemsSource="{Binding Categories}" SelectionChanged="favoris_SelectionChanged" Margin="10,10,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Foreground="#555" Background="Red" Loaded="CheckBox_Loaded" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您需要为列表框添加 ItemsSource 属性,而不是直接添加到复选框
这一定会对你有所帮助..
我正在尝试在我的应用程序中使用模式 MVVM Windows Phone。但是我在绑定列表框内的 CheckBox 时遇到了问题。
这是我的。xaml
<ListBox x:Name="LstbTagsFavoris" SelectionChanged="favoris_SelectionChanged" Margin="10,10,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Foreground="#555" Background="Red" Loaded="CheckBox_Loaded" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" Content="{Binding Categories}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我的视图模型
public class CategorieViewModel
{
private List<string> _Categories = new List<string>();
public List<string> Categories
{
get
{
return _Categories;
}
set
{
_Categories = value;
}
}
public void GetCategories()
{
Categories = GlobalVar._GlobalItem.SelectMany(a => a.tags)
.OrderBy(t => t)
.Distinct()
.ToList();
}
在我的 xaml.cs 中:
CategorieViewModel c = new CategorieViewModel();
c.GetCategories();
this.DataContext = c;
但是没用
实现 INotifyPropertyChanged 接口。
这样做。
public class CategorieViewModel : INotifyPropertyChanged
{
private List<string> _Categories = new List<string>();
public List<string> Categories
{
get
{
return _Categories;
}
set
{
_Categories = value;
OnPropertyChanged("Categories");
}
}
public void GetCategories()
{
Categories = GlobalVar._GlobalItem.SelectMany(a => a.tags)
.OrderBy(t => t)
.Distinct()
.ToList();
}
protected void OnPropertyChanged(string prop)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
在XAML代码中:
<ListBox x:Name="LstbTagsFavoris" ItemsSource="{Binding Categories}" SelectionChanged="favoris_SelectionChanged" Margin="10,10,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Foreground="#555" Background="Red" Loaded="CheckBox_Loaded" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" Content="{Binding}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您需要为列表框添加 ItemsSource 属性,而不是直接添加到复选框
这一定会对你有所帮助..