How to custom format data in datagridview during databinding

前端 未结 7 767
忘掉有多难
忘掉有多难 2020-12-16 14:21

I\'m looking for a way to format DataGridViewTextBoxColumn so that the value to be databinded is formatted during databinding. For example I have a CompanyName property and

相关标签:
7条回答
  • 2020-12-16 15:16

    I generally use ValueConverters for this sort of behavior.

    something like:

    <DataGridTextColumn Binding={Binding CompanyName, Converter={StaticResource CompanyNameShortenerConverter}} />
    

    In the control/page's resources node, you'll need to add something like:

    <local:CompanyNameConverter x:Key="CompanyNameShortenerConverter" />
    

    CompanyNameShortenerConverter should implement IValueConverter, and you can add the logic to "shorten" the company names passed in in the "Convert" method.

    This keeps the formatting/UI logic separate from the business logic (i.e. no need to add a "helper property" that shortens the name).

    0 讨论(0)
提交回复
热议问题