How to bind a boolean value to checkboxcolumn datagrid WPF
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 Tick开发者_运维百科ettable 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
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:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataGrid dgTickets = new DataGrid();
ObservableCollection<ISSUE> Issues = new ObservableCollection<ISSUE>(); // better an ObservableCollection than a List here, so that it updates correctly when you modify the content.
dgTickets.DataContext = Issues;
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") });
dgTickets.Columns.Add(new DataGridCheckBoxColumn {
Header = "To be invoiced?",
Binding = new Binding("IM_ITEM_CODE") { Converter = new ItemCodeToBoolConverter() }
});
}
}
/// <summary>
/// converts the item code string into a boolean
/// </summary>
public class ItemCodeToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string itemCode = (string)value;
return (itemCode == "TO BE INVOICED");
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool toBeInvoiced = (bool)value;
return toBeInvoiced ? "TO BE INVOICED" : "NOT TB INVOICED OR OFFER";
}
}
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:
<DataGrid ItemsSource="{Binding List}" >
And you can define templates for what is in the datagrid. In this case:
<DataGrid.Columns>
<!-- definition of the previous columns -->
<DataGridCheckBoxColumn>
<!-- Details on your checkbox here -->
</DataGridCheckBoxColumn>
</DataGrid.Columns>
If you bind the checkBox to your boolean, it will be directly checked or not, according to the boolean value :-)
精彩评论