简体   繁体   中英

How to dynamically set the Row's text to bold, using MVVM, C# and no Code-Behind?

I'm developing a WPF Page using .NET, MVVM, no code-behind, using PropertyChanged. In this Page, I have a DataGrid with a lot of columns. Into the DB, one of the Columns, let's call it HIGHLIGHT, will have values S or N. If value = S, the entire line will be Bold, or ExtraBold. Case N = Normal.

I made this work using this code in XAML:

        <Style x:Key="TextRowStyle" TargetType="{x:Type TextBlock}" >
        <Style.Triggers>
            <DataTrigger Binding="{Binding Slab.Highlight}" Value="S">
                <Setter Property="FontWeight" Value="ExtraBold"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

But doing in this way, I'll have to put in EVERY COLUMN, this code to make it work (Notice the ElementStyle):

<DataGridTextColumn Header="Test" Binding="{Binding SlabSeq}" ElementStyle="{StaticResource TextRowStyle}"/>

What do I need:

Each table of my DB has several Columns, over 60, 70, and i'm searching for a way to make this more easy, like the StaticResource TextRowStyle i've made...

Another thing i've made, it was a Converter:

public class HighlightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (System.Convert.ToChar(value).Equals("S"))
            return FontWeights.ExtraBold;
        else
            return FontWeights.Normal;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return FontWeights.ExtraBold;
    }
}

I'd tried to make a converter inside those fields, using:

<Page.Resources>
    <vm:HighlightConverter x:Key="HighlightConverter"/>
</Page.Resources>

and into the Grid:

<TextBlock FontWeight={Binding Slab.Highlight, Converter={StaticResource HighlightConverter}}"/>

Does anyone have any idea of how I can make this work?

Best regards, Gustavo.

why you dont create a "local" style in your DataGrid.Resources. did i get it right that the entire row has to be bold (S) or normal (N)?

<DataGrid.Resources>
  <Style TargetType="{x:Type DataGridCell}" >
     <Setter Property="FontWeight" Value="Normal"/>
     <Style.Triggers>
        <DataTrigger Binding="{Binding Slab.Highlight}" Value="S">
            <Setter Property="FontWeight" Value="ExtraBold"/>
        </DataTrigger>
    </Style.Triggers>
  </Style>
</DataGrid.Resources>

with the code above all cells would be normal, but if a datarow has Slab.Highlight=S all cells would be bold. the code is not tested. maybe you have to add TextBlock.Fontweight or something like that.

What about applying the style implicitly by dropping the x:Key ? That should make it apply everywhere in the grid when placed in the DataGrid.Resources .

This might work. Sorry, it's untested as I'm nowhere near an IDE.

            <DataGrid>
            <DataGrid.Resources>
                <Style TargetType="DataGridTextColumn">
                    <Style.Resources>
                        <Style TargetType="TextBlock">
                            <Setter Property="FontWeight" Value="{Binding Slab.Highlight, Converter={StaticResource HighlightConverter}}"/> 
                        </Style>
                    </Style.Resources>
                </Style>
            </DataGrid.Resources>
        </DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM