Wpf 4 DataGrid 列绑定问题

发布于 2024-09-18 05:18:00 字数 1203 浏览 5 评论 0原文

我创建了一个数据网格,其中的列绑定到可观察的集合。除了绑定到我的业务对象中可为空的十进制属性的列之外,一切都运行良好。

但是,如果我用作

<DataGridTextColumn Binding="{Binding Path=BaseUnitCostValue}" Header="Unit Cost Value" MinWidth="100" />

我的列定义,一切都很好,因为我最终希望它成为一个复杂的列,我尝试使用 DatagridTemplateColumn 这样,

<DataGridTemplateColumn Header="Unit Cost Value" MinWidth="100">
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

但是,使用此列配置,尽管我可以在完成后立即输入一个值对单元格进行编辑,其值消失。

我还尝试使用转换器转换为字符串,然后再次转换为可为空的小数,但没有成功。

我强烈怀疑这与它绑定到可为空的小数这一事实有关。我是否需要对 cellTemplates 做更多的事情,以便正确绑定值,就像使用标准 DataGridTextColumn 时一样?

谢谢

I have created a datagrid with columns bound to an observable collection. All works well except for a column which is bound to a nullable decimal property from my business object.

If I use

<DataGridTextColumn Binding="{Binding Path=BaseUnitCostValue}" Header="Unit Cost Value" MinWidth="100" />

as my column definition all works well, however, as I will eventually want it to be a complex column I tried using a DatagridTemplateColumn such that

<DataGridTemplateColumn Header="Unit Cost Value" MinWidth="100">
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

However, using this the column configuration, although I can enter a value as soon as I finish the edit to the cell, its value disappears.

I've also tried using converters to convert to a string and back again to a nullable decimal but with no luck.

I strongly suspect that this is something to with the fact that it's bound to a nullable decimal. Is there something more I need to do to my cellTemplates so that the value binds correctly, in the same way is does when using a standard DataGridTextColumn?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

雨落□心尘 2024-09-25 05:18:00

可能是因为您尝试在非编辑模板中对 TextBlock 进行双向绑定:

 <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
            <TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/> 
        </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 

尝试将其更改为单向,可能会起作用。还删除触发语句。

如果它没有帮助,请查看输出窗口,如果您看到相关消息。

Maybe it's because you try to make a two-way-binding for the TextBlock in the non-edit Template:

 <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
            <TextBlock Text="{Binding BaseUnitCostValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"/> 
        </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 

Try to change it to one-way, may it works then. Remove also the Trigger-statement.

If it does not help, look in the output-window, if you see a related message.

葵雨 2024-09-25 05:18:00

这是我的商业模式的问题。

我不得不将我的财产从

 Public Property BaseUnitCostValue As Decimal?
        Get
            Return _BaseUnitCostValue
        End Get
        Set(ByVal value As Decimal?)
            If _BaseUnitCostValue <> value Then
                _BaseUnitCostValue = value
                Me.DataStateChanged("BaseUnitCostValue")
                Me.DataStateChanged("TotalBaseUnitCostValue")
            End If

        End Set
    End Property

To

 Public Property BaseUnitCostValue As Decimal?
        Get
            Return _BaseUnitCostValue
        End Get
        Set(ByVal value As Decimal?)
            If (Not _BaseUnitCostValue.HasValue) OrElse (Not value.HasValue) OrElse _BaseUnitCostValue <> value Then
                _BaseUnitCostValue = value
                Me.DataStateChanged("BaseUnitCostValue")
                Me.DataStateChanged("TotalBaseUnitCostValue")
            End If

        End Set
    End Property

更改为 问题就消失了。感谢 HCL,因为简单转换器中断点的想法向我表明该属性实际上从未被更新。

It was problem in my business model.

I had to change my property from

 Public Property BaseUnitCostValue As Decimal?
        Get
            Return _BaseUnitCostValue
        End Get
        Set(ByVal value As Decimal?)
            If _BaseUnitCostValue <> value Then
                _BaseUnitCostValue = value
                Me.DataStateChanged("BaseUnitCostValue")
                Me.DataStateChanged("TotalBaseUnitCostValue")
            End If

        End Set
    End Property

To

 Public Property BaseUnitCostValue As Decimal?
        Get
            Return _BaseUnitCostValue
        End Get
        Set(ByVal value As Decimal?)
            If (Not _BaseUnitCostValue.HasValue) OrElse (Not value.HasValue) OrElse _BaseUnitCostValue <> value Then
                _BaseUnitCostValue = value
                Me.DataStateChanged("BaseUnitCostValue")
                Me.DataStateChanged("TotalBaseUnitCostValue")
            End If

        End Set
    End Property

And the problem went away. Thanks to HCL as the idea of the breakpoint in a simple converter showed me that the property was never actually being updated.

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