【问题标题】:How to add binding to a column in a datagrid?如何将绑定添加到数据网格中的列?
【发布时间】:2012-10-07 16:32:38
【问题描述】:

我在我的项目中遇到了绑定问题。 我有一个数据网格,默认情况下我定义了 2 列。 ItemsSource 绑定到列表 (ListeGrilleTarifaire),列的内容绑定到包含在第一个列表中的第二个列表 (ListeTranche) 的元素。

<techuc:DataGridM Name="dg_HeaderTranche" AutoGenerateColumns="False" ItemsSource="{Binding ListeGrilleTarifaire}" CanUserAddRows="False" CanUserDeleteRows="False" Visibility="{Binding accesGrilleMultiDepartement}" Height="42"  Margin="440,0,0,0" VerticalAlignment="Top">
<DataGrid.Columns>                                                   
    <DataGridTextColumn Header="{x:Static trad:Langues.lblTranche1}" Binding="{Binding ListeTranche[0],Mode=TwoWay}" Width="80"/>
    <DataGridTextColumn Header="{x:Static trad:Langues.lblTranche2}" Binding="{Binding ListeTranche[1],Mode=TwoWay}" Width="80"/>
</DataGrid.Columns>

我希望可以通过按钮添加列并将新列与第二个列表绑定。

我的代码在后面,当我点击按钮时,我将一个元素添加到我的第二个列表中并执行以下代码

DataGridTextColumn textColumnTranche = new DataGridTextColumn();
textColumnTranche.Header = "Tranche X";
textColumnTranche.Binding = new Binding("ListeTranche[3]");
dg_HeaderTranche.Columns.Add(textColumnTranche);

我的新列已创建,我可以填充它,但是当我将数据保存在数据库中时,值仍为 0。我认为这是因为我没有将绑定模式配置为 TwoWay,但我不知道如何在后面的代码中执行此操作。

我的列表在 ViewModel 中定义和使用。

【问题讨论】:

    标签: c# wpf xaml binding datagrid


    【解决方案1】:

    如何设置BindingMode = TwoWay:

    textColumnTranche.Binding = new Binding("ListeTranche[3]") { Mode = BindingMode.TwoWay };
    

    【讨论】:

    • 您为什么要删除答案标记?没有解决你的问题吗?
    猜你喜欢
    • 2016-01-20
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    相关资源
    最近更新 更多