对于模糊的描述感到抱歉,我想不出更好的方法。
假设我的ViewModel具有如下属性:
public List<MyClass> SubSystems { get; set; }
和SubSystems类:
public class SubSystem
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
在视图中,我想将SubSystems
属性绑定到我认为可能的复选框列表,其中CheckBox的IsChecked和Name属性绑定到它们各自的属性,IsChecked for IsSelected和Content for Name。
我知道我可以在XAML中创建一个ListBox,但我不确定如何使用绑定和集合来实现这一点..
感谢您的帮助!
编辑 -
这是XAML:
<GroupBox Header="Sub-Systems" Grid.Column="0" Grid.Row="0" Margin="5">
<Grid>
<Grid.Resources>
<DataTemplate x:Key="checkBox">
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</Grid.Resources>
<ListBox ItemTemplate="{StaticResource checkBox}" ItemsSource="{Binding SubSystems}" />
</Grid>
</GroupBox>
编辑#2 -
只是为了澄清一下,所有的例子都填充了这个框,但没有一个例子打破了setter中的断点。
答案 0 :(得分:2)
这个怎么样:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked={Binding IsSelected, Mode=TwoWay} /><TextBlock Text={Binding Name} />
</StackPanel>
</DataTemplate>
答案 1 :(得分:1)
修改ListBox.ItemTemplate以使用复选框,并将CheckBox.IsChecked绑定到SubSystem.IsSelected并将CheckBox.Content绑定到SubSystem.Name:
XAML:
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Name}" Margin="5" Focusable="False" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#:
private void window1_Loaded(object sender, RoutedEventArgs e)
{
this.SubSystems = new List<SubSystem>();
this.SubSystems.Add(new SubSystem() { Name = "SubSystem 1", IsSelected = false });
this.SubSystems.Add(new SubSystem() { Name = "SubSystem 2", IsSelected = false });
this.SubSystems.Add(new SubSystem() { Name = "SubSystem 3", IsSelected = true });
this.SubSystems.Add(new SubSystem() { Name = "SubSystem 4", IsSelected = false });
this.SubSystems.Add(new SubSystem() { Name = "SubSystem 5", IsSelected = true });
this.DataContext = this.SubSystems;
}
并确保将Checkable =“False”设置为CheckBoxes,否则您的用户就可以将其添加到其中。
修改强>
同样根据您添加的内容,您可能缺少ElementName属性(如果SubSystems不是窗口的DataContext,则需要使用ElementName绑定属性指定SubSystems属性的来源):
<ListBox ItemTemplate="{StaticResource checkBox}" ItemsSource="{Binding ElementName=window1, Path=SubSystems}" />
答案 2 :(得分:1)
我认为您可能需要ListBox
而不是ItemsControl
。 ListBoxes假设您要选择SubSystem
中的一个,但实际上,您只想使用数据模板排列项目:
<ItemsControl ItemsSource="{Binding SubSystems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Checkbox IsChecked="{Binding IsSelected, Mode=TwoWay}" Content="{Binding Name}" />
</DataTemplate>
<ItemsControl.ItemTemplate>
</ItemsControl>
答案 3 :(得分:1)
你的意思是这样吗?
子系统类
public class SubSystem : INotifyPropertyChanged
{
private string mName;
private Boolean mIsSelected = false;
public SubSystem()
{
}
public SubSystem(string name, Boolean isSelected)
{
this.Name = name;
this.IsSelected = isSelected;
}
public string Name
{
get { return mName; }
set
{
if (mName != value)
{
mName = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
public Boolean IsSelected
{
get { return mIsSelected; }
set
{
if (mIsSelected != value)
{
mIsSelected = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
<强>视图模型强>
ObservableCollection<SubSystem> mSubSystems = new ObservableCollection<SubSystem>();
public ObservableCollection<SubSystem> SubSystems
{
get { return mSubSystems; }
set { mSubSystems = value; }
}
查看强>
<ListBox x:Name="lstSubsystems" ItemsSource="{Binding SubSystems}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}">
<ContentPresenter Content="{Binding Name}" />
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
希望有所帮助, WTS