Y
Y
youngpr2020-10-13 19:41:29
WPF
youngpr, 2020-10-13 19:41:29

How to set the appearance of a certain field in the DataGrid?

I created a directory, there are such fields as full name, position, number, etc. I would like to set the design in the form of a color to a certain field of this column, for example, there is a full name column, and somewhere there is a field Andrey Kravtsov and I would like to set a color for this field, how to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2020-10-15
@yarosroman

Create your own converter ( https://www.wpf-tutorial.com/en/39/%D1%81%D0%B2%D1... ) and bind to the background property of your top element in the cell template, and specify the converter , and in the converter you return the corresponding color value. If it's completely unclear, I'll write the code for you.

B
Boris the Animal, 2020-10-24
@Casper-SC

You can't put a lot of code here, the rest is in the comments. This example doesn't rely heavily on Mvvm, so the window code is a collection, not a model view. The MvvmLight library is used.
Views/MainWindow.xaml

<Window
    x:Class="WpfDataGrid.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:viewModels="clr-namespace:WpfDataGrid.ViewModels"
    Title="MainWindow"
    Width="800"
    Height="450"
    DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource Self}}"
    WindowStartupLocation="CenterScreen"
    mc:Ignorable="d">
    <Window.Resources>

        <SolidColorBrush
            x:Key="BackgroundOfSelectedDataGridRow"
            Color="#B6B6B6" />
        <SolidColorBrush
            x:Key="BorderBrushOfSelectedDataGridRow"
            Color="#FF2485C9" />
        <SolidColorBrush
            x:Key="ForegroundOfSeletedDataGridRow"
            Color="Black" />

        <SolidColorBrush
            x:Key="GridLinesBrush"
            Color="#FFB0B0B0" />

        <FontFamily x:Key="DefaultFontFamylyKey">
            Microsoft Sans Serif
        </FontFamily>

        <Style
            x:Key="DefaultDataGridCellStyle"
            TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="#B2FFD0A2" />
                    <Setter Property="BorderBrush" Value="#99FFE5CC" />
                    <Setter Property="Foreground" Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
        <Style
            x:Key="DataGridStyle"
            TargetType="{x:Type DataGrid}">
            <Setter Property="RowBackground" Value="#FFE6E6E6" />
            <Setter Property="AlternatingRowBackground" Value="#FFF1F1F1" />
            <Setter Property="AlternationCount" Value="2" />
            <Setter Property="HorizontalGridLinesBrush" Value="{StaticResource GridLinesBrush}" />
            <Setter Property="VerticalGridLinesBrush" Value="{StaticResource GridLinesBrush}" />
            <Setter Property="FontFamily" Value="{StaticResource DefaultFontFamylyKey}" />
            <Setter Property="FontSize" Value="15" />
            <Setter Property="SelectionMode" Value="Single" />
            <Setter Property="RowStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type DataGridRow}">
                        <Style.Triggers>

                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="{StaticResource BackgroundOfSelectedDataGridRow}" />
                                <Setter Property="BorderBrush" Value="{StaticResource BorderBrushOfSelectedDataGridRow}" />
                                <Setter Property="Foreground" Value="{StaticResource ForegroundOfSeletedDataGridRow}" />
                                <Setter Property="Tag" Value="{Binding}" />
                            </Trigger>

                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="#B2FFD0A2" />
                                <Setter Property="BorderBrush" Value="#99FFE5CC" />
                                <Setter Property="Foreground" Value="Black" />
                            </Trigger>

                        </Style.Triggers>
                    </Style>

                </Setter.Value>
            </Setter>

            <Setter Property="CellStyle" Value="{StaticResource DefaultDataGridCellStyle}" />

        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <DataGrid
            Margin="10,10,10,0"
            AutoGenerateColumns="False"
            ItemsSource="{Binding Collection}"
            Style="{StaticResource DataGridStyle}">
            <DataGrid.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel
                        IsVirtualizing="True"
                        VirtualizationMode="Recycling" />
                </ItemsPanelTemplate>
            </DataGrid.ItemsPanel>

            <DataGrid.Columns>
                <DataGridTemplateColumn
                    x:Name="PropertiesUpdateIndicatorColumn"
                    CanUserReorder="False"
                    CanUserResize="False"
                    CanUserSort="False"
                    IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="viewModels:ItemViewModel">
                            <Rectangle Fill="{Binding IsChecked, Converter={StaticResource BooleanToBrushConverter}}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate DataType="viewModels:ItemViewModel">
                            <TextBlock Text="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn
                    Width="50"
                    ClipboardContentBinding="{x:Null}"
                    Header="Выбран">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="viewModels:ItemViewModel">
                            <CheckBox
                                HorizontalContentAlignment="Center"
                                VerticalContentAlignment="Center"
                                IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.HeaderTemplate>
                        <ItemContainerTemplate>
                            <TextBlock
                                Text=""
                                ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"
                                ToolTipService.HasDropShadow="False"
                                ToolTipService.Placement="Relative">
                                <TextBlock.Resources>
                                    <Style TargetType="ToolTip">
                                        <Setter Property="VerticalOffset" Value="-1" />
                                        <Setter Property="HorizontalOffset" Value="-1" />
                                        <Setter Property="OverridesDefaultStyle" Value="True" />
                                        <Setter Property="HasDropShadow" Value="False" />
                                    </Style>
                                </TextBlock.Resources>
                            </TextBlock>
                        </ItemContainerTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn
                    x:Name="TitleColumn"
                    ClipboardContentBinding="{x:Null}"
                    Header="Название"
                    SortMemberPath="Title">
                    <DataGridTemplateColumn.HeaderTemplate>
                        <ItemContainerTemplate>
                            <TextBlock
                                Text="Название"
                                ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"
                                ToolTipService.HasDropShadow="False"
                                ToolTipService.Placement="Relative">
                                <TextBlock.Resources>
                                    <Style TargetType="ToolTip">
                                        <Setter Property="VerticalOffset" Value="-1" />
                                        <Setter Property="HorizontalOffset" Value="-1" />
                                        <Setter Property="OverridesDefaultStyle" Value="True" />
                                        <Setter Property="HasDropShadow" Value="False" />
                                    </Style>
                                </TextBlock.Resources>
                            </TextBlock>
                        </ItemContainerTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Title, Mode=OneWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <TextBox
                                MaxLength="120"
                                Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

        <Button
            Grid.Row="1"
            Width="75"
            Margin="10,6,0,10"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Click="OnDeleteButtonClick"
            Content="Delete" />
    </Grid>
</Window>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question