如何将ID从数据库对象存储到WPF之后的代码中的复选框 [英] How to store ID from database object, to a checkbox in code behind WPF
问题描述
我正在使用WPF应用程序,并且我有用户,并且用户当然具有某种角色,例如SUPERADMIN和ADMIN,这些角色存储在表"Roles"中,一个用户可以具有1个或多个角色,这意味着可以在以这种方式动态生成的表单复选框上选中一个或多个复选框:
I'm working on WPF application, and I have users, and ofcourse users has some kind of roles, in my case SUPERADMIN AND ADMIN, that roles are stored in table "Roles", One user could have 1 or more roles, so that means one or more checkbox can be selected on my form checkboxes that I generated dynamically on this way:
private void LoadRolesToStackPanel()
{
try
{
var roles = RolesController.Instance.SelectAll();
if (roles.Count > 0)
{
foreach (Role r in roles)
{
CheckBox cb = new CheckBox();
//cb.Name = r.RoleId.ToString();
cb.Content = r.Title.ToString();
cb.FontSize = 15;
stackRole.Children.Add(cb);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
稍后,当我保存用户时,我需要从选中的组合框中获取ID,因为它准确地表示了Role,因此我需要使用相应的RoleId将其插入数据库.
Later when I'm saving User I need to get ID from checked combobox because it is representing Role acctually, so I need to make insert to database with corresponding RoleId..
这是我在事件SaveUser上选中的复选框的方式:
Here is how I took selected checkboxes on event SaveUser:
IEnumerable<CheckBox> selectedBoxes =
from checkbox in this.stackRole.Children.OfType<CheckBox>()
where checkbox.IsChecked.Value
select checkbox;
foreach (CheckBox box in selectedBoxes)
{
// do something
}
有可能注意到我可以从组合框中获取标题,因为我说过
As it is possible to notice I can get Title from combobox because I said
cb.Content = r.Title.ToString();
但是我还需要ID为"r"-角色,并且我不知道如何以添加标题的方式将其添加到复选框.
But I need ID of "r" - role also, and I don't know how to add it to a checkbox on way I added Title.
推荐答案
解决此问题的正确"方法是使用绑定到IEnumerable<Role>
的ItemsControl
:
The "correct" way of solving this would be to use an ItemsControl
that you bind to an IEnumerable<Role>
:
<ItemsControl ItemsSource="{Binding Roles}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Title}" FontSize="15" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public partial class MainWindow : Window
{
private readonly ViewModel _viewModel = new ViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _viewModel;
}
}
public class ViewModel
{
public ViewModel()
{
Roles = RolesController.Instance.SelectAll();
}
public List<Role> Roles { get; private set; }
}
这意味着您应该在Role
类中添加一个IsChecked
属性(或创建一个具有此属性的特定于新的客户端应用程序的Role
类,并使用该属性代替当前的Role
类).
This means that you should add an IsChecked
property to the Role
class (or create a new client application specific Role
class that has this property and use this one instead of your current Role
class).
然后,您可以在视图的代码背后或视图模型中遍历所选角色:
You could then iterate through the selected roles, either in the code-behind of the view or in the view model:
foreach (var role in _viewModel.Roles)
{
if(role.IsSelected)
{
//...
}
}
该解决方案基于MVVM,这是 推荐的设计模式,可用于开发基于XAML的用户界面应用程序.您应该学习它.您可以在此处了解更多信息: https://msdn.microsoft.com/zh-cn/library/hh848246.aspx .
This solution is built on MVVM which is the recommended design pattern to use when developing XAML based user interface applications. You should learn it. You could read more about it here: https://msdn.microsoft.com/en-us/library/hh848246.aspx.
这篇关于如何将ID从数据库对象存储到WPF之后的代码中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!