<WrapPanel Orientation="Horizontal" x:Name="wplContent"/>
添加控件
private CheckBox GenerateCheckBox(string name, string tag = "")
{
var cb = new CheckBox
{
Margin = new Thickness(5, 5, 5, 5),
IsChecked = false,
Content = name,
Tag = tag,
};
return cb;
}
public void AddCheckBox(string name, string tag)
{
var cb = GenerateCheckBox(name, tag);
wplContent.Children.Add(cb);
}
删除控件
public void RemoveCheckBox(string tag)
{
var item = wplContent.Children.OfType<CheckBox>().FirstOrDefault(x => x.Tag.ToString() == tag);
if (item != null)
{
wplContent.Children.Remove(item);
}
}
是否都没选择
/// <summary>
/// 是否都没选择
/// </summary>
/// <returns>true都没选, false至少有一个勾选</returns>
public bool IsAllUnSelected()
{
var list = wplContent.Children.OfType<CheckBox>();
return list.Any(x => x.IsChecked == true);
}
获取所有的显示内容
/// <summary>
/// 获取所有的显示内容
/// </summary>
/// <returns>形如: 1,2,3</returns>
public string GetNames()
{
var list = wplContent.Children.OfType<CheckBox>();
var arr = list.Select(s => s.Name).ToArray();
return string.Join(",", arr);
}
设置选中
/// <summary>
/// 设置选中
/// </summary>
/// <param name="tags"></param>
public void SetChecked(IEnumerable<string> tags)
{
foreach (var item in wplContent.Children.OfType<CheckBox>())
{
if (tags.Contains(item.Tag.ToString()))
{
item.IsChecked = true;
}
else
{
item.IsChecked = false;
}
}
}