如何将布尔值绑定到复选框列数据网格WPF

发布于 2024-10-25 19:12:16 字数 1485 浏览 5 评论 0原文

我正在一家学校公司开展一个项目,我必须制作一个包含票证信息的数据网格: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亽野灬性zι浪 2024-11-01 19:12:19

我真的不明白你对布尔列表的需要。

我会使用转换器并直接绑定到该属性。像这样的东西:

   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";
        }
    }

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";
        }
    }
恬淡成诗 2024-11-01 19:12:19

除了前面的答案之外,我建议您更多地使用 XAML。 WPF最大的优势就是可以轻松拥有独立的视图和视图。模型(感谢 MVVM 模式)

在这种情况下,您有一个显示自定义对象内容的 DataGrid,对吗?

首先,您熟悉数据绑定和OnPropertyChanged接口吗?如果没有,您一定应该尝试更多地了解它,这真的很容易使用。

我这样做的方法是在 ViewModel 中添加一个属性“列表”,其中包含要显示的对象的列表。

然后,您可以声明数据网格并将其绑定到列表:

<DataGrid ItemsSource="{Binding List}" >

并且您可以为数据网格中的内容定义模板。在这种情况下:

<DataGrid.Columns>
   <!-- definition of the previous columns -->
   <DataGridCheckBoxColumn>
      <!-- Details on your checkbox here --> 
   </DataGridCheckBoxColumn> 
</DataGrid.Columns>

如果您将复选框绑定到您的布尔值,则根据布尔值,将直接检查或不检查它:-)

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文