Search code examples
c#wpfdatagridforegroundmaterial-design-in-xaml

How set Foreground with binding on materialdesign:MaterialDataGridTextColumn


I can't set Foreground of a columncell with binding, if I use that syntax in xaml:

<materialDesign:MaterialDataGridTextColumn Foreground="{Binding Foreground}"/>

On windows load in the output terminal I see that:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Foreground; DataItem=null; target element is 'MaterialDataGridTextColumn' (HashCode=29149718); target property is 'Foreground' (type 'Brush')

If I put the direct color for example:

<materialDesign:MaterialDataGridTextColumn Foreground="Red"/>

The Datagrid Cell render foreground correctly, did anyone have some suggestion?

Best Regards

Franco


Solution

  • I found the solution!

    I have to work with ElementStyle property:

    <DataGridTextColumn Binding="{Binding Name}">
        <DataGridTextColumn.ElementStyle>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="{Binding Foreground}"/>
            </Style>
        </DataGridTextColumn.ElementStyle>
    </DataGridTextColumn>
    

    I Hope this is usefull to someone else.

    Regards

    Franco