Share via

Question for DataGridTextColumn.ForegroundProperty binding.

DaisyTian-1203 11,651 Reputation points Moderator
2021-12-09T02:11:59.04+00:00

From the document Remark part , I knew the below code will not work for Foreground binding

  <DataGridTextColumn Header="FontSize" Width="80" Binding="{Binding FontSize}" Foreground="{Binding ForeColor}"/>  

I checked the DataGridTextColumn and the TextBlock and found the latter has more code than the former, is that the reason?Is there any body know the reason for it?

 public static void SetForeground(DependencyObject element, Brush value)  
        {  
            if (element == null)  
            {  
                throw new ArgumentNullException("element");  
            }  
   
            element.SetValue(ForegroundProperty, value);  
        }  
  
  
 public static Brush GetForeground(DependencyObject element)  
        {  
            if (element == null)  
            {  
                throw new ArgumentNullException("element");  
            }  
   
            return (Brush)element.GetValue(ForegroundProperty);  
        }  

I doesn't need any workarounds just want to know the reason, I know workarounds, like below:

 <DataGridTemplateColumn Header="FontSize " Width="80" CanUserSort="True"  >  
                        <DataGridTemplateColumn.CellTemplate>  
                            <DataTemplate>  
                                <TextBox  Text="{Binding  FontSize}" Foreground="{Binding ForeColor}"  HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderThickness="0" VerticalAlignment="Center"  />  
                            </DataTemplate>  
                        </DataGridTemplateColumn.CellTemplate>  
                    </DataGridTemplateColumn>  

Thanks for any help.

Developer technologies | Windows Presentation Foundation
0 comments No comments

Answer accepted by question author

Peter Fleischer (former MVP) 19,351 Reputation points
2021-12-09T06:20:45.22+00:00

Hi,
set the element style to use ForeColor property from data item:

    <DataGridTextColumn Header="FontSize"
                        Width="80" 
                        Binding="{Binding FontSize}">
      <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
          <Setter Property="Foreground" Value="{Binding ForeColor}" />
        </Style>
      </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>

or use Proxy for ForeColor property from ViewModel (DataContext):

<FrameworkElement x:Name="ProxyElement" Visibility="Collapsed"/>
<DataGrid ItemsSource="{Binding View}" AutoGenerateColumns="False" CanUserAddRows="False">
  <DataGrid.Columns>
    <DataGridTextColumn Header="FontSize"
                        Width="80" 
                        Binding="{Binding FontSize}"
                        Foreground="{Binding DataContext.ForeColor, Source={x:Reference ProxyElement}}"/>

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2021-12-09T08:41:24.493+00:00

    The foreground here applies to columns, not row (or cell) units, the data context is not row-by-row data. You could also bind CellStyle, which is a cell.

     <DataGridTextColumn Header="FontSize" Binding="{Binding FontSize}">  
                        <DataGridTextColumn.CellStyle>  
                            <Style TargetType="DataGridCell">  
                                <Setter Property="Foreground" Value="{Binding  ForeColor}"/>  
                                <Setter Property="FontSize" Value="{Binding FontSize}"/>  
                            </Style>  
                        </DataGridTextColumn.CellStyle>  
                    </DataGridTextColumn>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.