WindowsCommunityToolkit
WindowsCommunityToolkit copied to clipboard
Binding to Foreground property on DataGridTextColumn breaks the app
Describe the bug
The Issue #2991 should not be closed if it was not solved.
This is still the same problem and there is an workaround - not use the DataGridTextColumn and use a template with the TextBlock inside that doesn't break the app.
That's why people are not complaining. But it changes the xaml
file from this:
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Tag="Code" Header="Code"
Binding="{Binding Code}"/>
<controls:DataGridTextColumn Tag="Stock" Header="Stock"
Binding="{Binding Stock}"/>
<controls:DataGridTextColumn Tag="Type" Header="Type"
Binding="{Binding Type}"/>
<controls:DataGridTextColumn Tag="TheoreticalQuantity" Header="TheoreticalQuantity"
Binding="{Binding TheoreticalQuantity, Converter={StaticResource StringFormat}, ConverterParameter=' {0:###,###}'}"/>
<controls:DataGridTextColumn Tag="Part" Header="Part"
Binding="{Binding Part, Converter={StaticResource PercentageValueDisplay}}"/>
<controls:DataGridTextColumn Tag="PrecoTeorico" Header="PrecoTeorico"
Binding="{Binding PrecoTeorico, Converter={StaticResource CurrencyDisplay}}"/>
<controls:DataGridTextColumn Tag="Cotacao" Header="Cotacao"
Binding="{Binding Cotacao, Converter={StaticResource CurrencyDisplay}}"/>
<!--Foreground="{Binding Variacao, Converter={StaticResource PositiveOrNegativeFormat}}"/-->
<controls:DataGridTextColumn Tag="Fechamento" Header="Fechamento"
Binding="{Binding Fechamento, Converter={StaticResource CurrencyDisplay}}"/>
<controls:DataGridTextColumn Tag="VariacaoTeorica" Header="VariacaoTeorica"
Binding="{Binding VariacaoTeorica, Converter={StaticResource PercentageDisplay}}"/>
<!--Foreground="{Binding VariacaoTeorica, Converter={StaticResource PositiveOrNegativeFormat}}"/-->
<controls:DataGridTextColumn Tag="Variacao" Header="Variacao"
Binding="{Binding Variacao, Converter={StaticResource PercentageDisplay}}"/>
<!--Foreground="{Binding Variacao, Converter={StaticResource PositiveOrNegativeFormat}}"/-->
</controls:DataGrid.Columns>
To This:
<controls:DataGrid.Columns>
<controls:DataGridTemplateColumn Tag="Code" Header="Code">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4" Text="{Binding Code}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="Stock" Header="Stock">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4" Text="{Binding Stock}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="Type" Header="Type">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4" Text="{Binding Type}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="TheoreticalQuantity" Header="TheoreticalQuantity">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4"
Text="{Binding TheoreticalQuantity, Converter={StaticResource StringFormat}, ConverterParameter=' {0:###,###}'}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="Part" Header="Part">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4"
Text="{Binding Part, Converter={StaticResource PercentageValueDisplay}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="PrecoTeorico" Header="PrecoTeorico">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4" Text="{Binding PrecoTeorico, Converter={StaticResource CurrencyDisplay}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="Cotacao" Header="Cotacao">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4"
Text="{Binding Cotacao, Converter={StaticResource CurrencyDisplay}}"
Foreground="{Binding Variacao, Converter={StaticResource PositiveOrNegativeFormat}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="Fechamento" Header="Fechamento">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4" Text="{Binding Fechamento, Converter={StaticResource CurrencyDisplay}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="VariacaoTeorica" Header="VariacaoTeorica">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4"
Text="{Binding VariacaoTeorica, Converter={StaticResource PercentageDisplay}}"
Foreground="{Binding VariacaoTeorica, Converter={StaticResource PositiveOrNegativeFormat}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Tag="Variacao" Header="Variacao">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Margin="4"
Text="{Binding Variacao, Converter={StaticResource PercentageDisplay}}"
Foreground="{Binding Variacao, Converter={StaticResource PositiveOrNegativeFormat}}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
Using the Foreground
property without binding, works flawlessly.
Regression
No response
Reproducible in sample app?
- [X] This bug can be reproduced in the sample app.
Steps to reproduce
Clone the sample app and try to bind something to the `Foreground` property of the `DataGridTextColumn` and it will break in the `InitializeComponent()` inside the constructor.
Expected behavior
It should not break the app and apply correctly the bindings / conversions.
Screenshots
Exception with the Foreground
binded.
It seems it's trying to use the bind object without send it to the converter and assigning it to the brush of the Foreground property.
Exceção gerada: 'System.InvalidCastException' em System.Private.CoreLib.dll
Exceção gerada em 0x00007FFC99A0478C (KernelBase.dll) em IBovTrackerWinUI.exe: WinRT originate error - 0x80004002 : 'Unable to cast object of type 'Microsoft.UI.Xaml.Data.Binding' to type 'Microsoft.UI.Xaml.Media.Brush'.'.
onecore\com\combase\winrt\error\restrictederror.cpp(1017)\combase.dll!00007FFC9A0EA21E: (caller: 00007FFC99FDA2F3) ReturnHr(1) tid(5810) 8007007E The specified module could not be found.
Exceção gerada em 0x00007FFC99A0478C (KernelBase.dll) em IBovTrackerWinUI.exe: WinRT originate error - 0x802B000A : 'Failed to assign to property 'CommunityToolkit.WinUI.UI.Controls.DataGridTextColumn.Foreground'. [Line: 175 Position: 20]'.
onecore\com\combase\winrt\error\restrictederror.cpp(1017)\combase.dll!00007FFC9A0EA21E: (caller: 00007FFC99FDA2F3) ReturnHr(2) tid(5810) 8007007E The specified module could not be found.
Exceção gerada: 'Microsoft.UI.Xaml.Markup.XamlParseException' em WinRT.Runtime.dll
Informações de WinRT: Failed to assign to property 'CommunityToolkit.WinUI.UI.Controls.DataGridTextColumn.Foreground'. [Line: 175 Position: 20]
XAML parsing failed.
Windows Build Number
- [ ] Windows 10 1809 (Build 17763)
- [ ] Windows 10 1903 (Build 18362)
- [ ] Windows 10 1909 (Build 18363)
- [ ] Windows 10 2004 (Build 19041)
- [ ] Windows 10 20H2 (Build 19042)
- [ ] Windows 10 21H1 (Build 19043)
- [X] Windows 11 21H2 (Build 22000)
- [ ] Other (specify)
Other Windows Build number
No response
App minimum and target SDK version
- [X] Windows 10, version 1809 (Build 17763)
- [ ] Windows 10, version 1903 (Build 18362)
- [ ] Windows 10, version 1909 (Build 18363)
- [ ] Windows 10, version 2004 (Build 19041)
- [ ] Other (specify)
Other SDK version
No response
Visual Studio Version
2022
Visual Studio Build Number
No response
Device form factor
No response
Nuget packages
No response
Additional context
No response
Help us help you
Yes, I'd like to be assigned to work on this item.
Hello Bigous, thank you for opening an issue with us!
I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.
This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.