如何解决如何在运行后选中用户控件上的所有复选框
我不知道这是否可行,但我正在制作一个系统,其中对于列表中的每个项目,它将解析信息并使用信息制作用户控件。我想知道如何或是否应该为此使用用户控件。我不想使用数据网格(如果可能)。我的目标是找到一种方法,在运行后单击按钮即可启用所有复选框(每个用户控件 1 个)。可能知道如何在用户控件上设置一个值也会很酷。
this is a example of the user control
this is what the user controls look like after the while loop
我对如何解决这个问题的想法是列出所有用户控件,但我不知道如何找到复选框并选中它。
这是我创建用户控件的代码。
JobLauncher
任何帮助都会很棒,谢谢!
解决方法
最好通过ItemsControl创建控件,而不是在后面的代码中
xml
<Window.Resources>
<ResourceDictionary>
<DataTemplate x:Key="ItemTemplate">
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ItemsControl ItemsSource="{Binding Users}" ItemTemplate="{StaticResource ItemTemplate}"/>
</Grid>
视图模型
public class UserModel
{
public string Name { get; set; }
public bool IsChecked { get; set; }
}
public class WpfTestVm
{
public ObservableCollection<UserModel> Users { get; } = new ObservableCollection<UserModel> {new UserModel { Name = "1"},new UserModel { Name = "2" } };
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。