我有 WPF ComboBox 包含 CheckBox 项目,它允许用户选择多个项目。
在用户从组合框中选择一个项目后,我想在组合框中将所选项目的列表显示为逗号分隔的文本。
XAML:
<ComboBox Name="cmbEnvironment" >
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}" Content="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
代码背后:
var lstEnvironment = new List<Environment>();
lstEnvironment.Add( new Environment() { Name = "Env1", IsSelected = false});
lstEnvironment.Add(new Environment() { Name = "Env2", IsSelected = false });
lstEnvironment.Add(new Environment() { Name = "Env3", IsSelected = false });
cmbEnvironment.ItemsSource = lstEnvironment;
模型:
public class Environment
{
public string Name { get; set; }
public bool IsSelected { get; set; }
}
我怎样才能做到这一点?