I have read that for better performance, you should apply opacity to the foreground/background brush rather than the entire element. That is what I am trying to do, but I cannot figure it out.
Here is my XAML that works, but is setting the entire TextBlock element opacity:
<DataGrid>
<DataGrid.Resources>
<local:OpacityConverter x:Key="OpacityConverterKey" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="1*" Binding="{Binding Number}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Opacity" Value="{Binding Number, Converter={StaticResource OpacityConverterKey}}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="0">
<Setter Property="Foreground" Value="Lime" />
</DataTrigger>
<DataTrigger Binding="{Binding Status}" Value="1">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Here was my attempt to bind the opacity for just the foreground brush of TextBlock:
<DataGrid>
<DataGrid.Resources>
<local:OpacityConverter x:Key="OpacityConverterKey" />
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="1*" Binding="{Binding Number}">
<TextBlock.Foreground>
<SolidColorBrush Color="Blue" Opacity="{Binding Distance, Converter={StaticResource OpacityConverterKey}}" />
</TextBlock.Foreground>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="0">
<Setter Property="Foreground" Value="Lime" />
</DataTrigger>
<DataTrigger Binding="{Binding Status}" Value="1">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
The converter does not work in this situation. Visual Studio underlines it and says "No DataContext found for binding." It doesn't have issue with the first example though.
How can I go about just setting the brush opacity?
Thanks for your time.