如何将布尔值绑定到复选框列数据网格WPF
我正在一家学校公司开展一个项目,我必须制作一个包含票证信息的数据网格:ID 号、票证描述。
在数据库中的 Tickettable 中,我有一列“Item”,它可以具有以下值:“不开具发票”、“要开具发票”或“报价”。数据网格中的第三列必须是复选框列,我的老板希望我可以选中该复选框,这意味着该票必须开具发票,当我取消选中时,这意味着“不开具发票”或“报价”,这并不重要。我的问题是当我绑定“ID”和“描述”字段时,我不知道如何将复选框列与“true 或 false”字段绑定?我只知道如何从工单绑定表字段,但我想添加一个布尔字段,这样我就可以在代码中确定何时“要开具发票”,我的布尔字段必须为 true,而当不开具发票时,它一定是假的,所以未经检查。
dgTickets.DataContext = new List<ISSUE>();
dgTickets.Columns.Add(new DataGridTextColumn { Header = "Id", Binding = new Binding("IM_ISSUE_NO") });
dgTickets.Columns.Add(new DataGridTextColumn { Header = "Description", Binding = new Binding("IM_DESCRIPTION") });
DataGridCheckBoxColumn chk = new DataGridCheckBoxColumn();
chk.Header = "To be invoiced?";
List<ISSUE> lTickets = new List<ISSUE>();
lTickets = _ISSUEBO.getTickets();
//here I want to make a list of booleans when the tickets are 'to be invoiced or not' in the database
List<bool> lChecks = new List<bool>();
int intTeller = 0;
bool boolFact = false;
foreach (ISSUE i in lTickets) {
switch (i.IM_ITEM_CODE) {
case "TO BE INVOICED":
boolFact = true;
break;
case "NOT TB INVOICED":
boolFact = false;
break;
case "OFFER":
boolFact = false;
break;
default: break;
}
lChecks.Add(boolFact);
intTeller++;
}
Binding b = new Binding("lChecks"); //??? this is probably wrong, but i don't know how to do
chk.Binding = b;
this.dgTickets.Columns.Add(chk);
dgTickets.ItemsSource = lTickets;
有人可以帮我吗?
提前致谢
i'm working on a project at a company for school and I have to make a datagrid with ticketinformation: The id-number, description of a ticket.
In my Tickettable in the database, I have a column 'Item' and it can have the values: "Not to be invoiced","To be invoiced" or "Offer". The 3rd column in the datagrid has to be a checkboxcolumn and my boss wants me that there is a possibility to check the checkbox, that means the ticket has to be invoiced, and when I uncheck, it means "not to be invoiced" or "offer", that doesn't really matter. My problem is when I bind the "ID" and "Description"-fields, I don't know how to bind the checkboxcolumn with a 'true or false' field? I only know how to bind tablefields from Ticket, but I want to add a boolean-field so I can determine in code when it's "to be invoiced", my boolean-field has to be true and when it's not to be invoiced, it has to be false, so unchecked.
dgTickets.DataContext = new List<ISSUE>();
dgTickets.Columns.Add(new DataGridTextColumn { Header = "Id", Binding = new Binding("IM_ISSUE_NO") });
dgTickets.Columns.Add(new DataGridTextColumn { Header = "Description", Binding = new Binding("IM_DESCRIPTION") });
DataGridCheckBoxColumn chk = new DataGridCheckBoxColumn();
chk.Header = "To be invoiced?";
List<ISSUE> lTickets = new List<ISSUE>();
lTickets = _ISSUEBO.getTickets();
//here I want to make a list of booleans when the tickets are 'to be invoiced or not' in the database
List<bool> lChecks = new List<bool>();
int intTeller = 0;
bool boolFact = false;
foreach (ISSUE i in lTickets) {
switch (i.IM_ITEM_CODE) {
case "TO BE INVOICED":
boolFact = true;
break;
case "NOT TB INVOICED":
boolFact = false;
break;
case "OFFER":
boolFact = false;
break;
default: break;
}
lChecks.Add(boolFact);
intTeller++;
}
Binding b = new Binding("lChecks"); //??? this is probably wrong, but i don't know how to do
chk.Binding = b;
this.dgTickets.Columns.Add(chk);
dgTickets.ItemsSource = lTickets;
Can anyone help me please?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的不明白你对布尔列表的需要。
我会使用转换器并直接绑定到该属性。像这样的东西:
I don't really understand your need for the boolean list.
I'd use a converter and bind directly to the property. Something like this:
除了前面的答案之外,我建议您更多地使用 XAML。 WPF最大的优势就是可以轻松拥有独立的视图和视图。模型(感谢 MVVM 模式)
在这种情况下,您有一个显示自定义对象内容的 DataGrid,对吗?
首先,您熟悉数据绑定和OnPropertyChanged接口吗?如果没有,您一定应该尝试更多地了解它,这真的很容易使用。
我这样做的方法是在 ViewModel 中添加一个属性“列表”,其中包含要显示的对象的列表。
然后,您可以声明数据网格并将其绑定到列表:
并且您可以为数据网格中的内容定义模板。在这种情况下:
如果您将复选框绑定到您的布尔值,则根据布尔值,将直接检查或不检查它:-)
In addition to the previous answer, I'd advise you to use more XAML. WPF's biggest advantage is that you can easily have independant view & model (thanks to the MVVM pattern)
In this case, you have a DataGrid which is displaying the content of a custom object, am I right?
First of all, are you familiar with data binding and OnPropertyChanged interface? If not you should definitely try to know more about that, which is really easy to use.
The way I'd do that is to add in your ViewModel a property "list", containing a list of the objects to be displayed.
Then, you can just declare your datagrid and bind it to the list:
And you can define templates for what is in the datagrid. In this case:
If you bind the checkBox to your boolean, it will be directly checked or not, according to the boolean value :-)