简体   繁体   中英

WPF - setting datagridcell background AND horizontal alignment messes up background

Via multibinding I was able to set the background of specific cells. However, I want to set the horizontalalignment of the cell text to right, but that messes up the background color that is supposed to stretch the complete background (not the aligned text only):

背景不好

Here's the minified (runnable) code

public partial class MainWindow : Window
{
    public ObservableCollection<Test> MyData { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;
        MyData = Test.GetData();
    }
}

public class Test
{
    public string Title { get; set; }
    public string ColOne { get; set; }
    public string ColTwo { get; set; }

    public static ObservableCollection<Test> GetData()
    {
        return new ObservableCollection<Test>
        {
            new Test { Title = "HO", ColOne = "3.20", ColTwo = "5.85"},
            new Test { Title = "DOR", ColOne = "-3.33", ColTwo = "5.9"}
        };
    }
}

and the XAML

<Window x:Class="ColorColumnAlignment.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid ItemsSource="{Binding Path=MyData}">
            <DataGrid.Resources>
                <Style TargetType="DataGridCell">
                    <Style.Setters>
                        <Setter Property="HorizontalAlignment" Value="Right"></Setter>
                        <Setter Property="Background" Value="Chocolate"></Setter>
                    </Style.Setters>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
    </Grid>
</Window>

I've seen someone with a similar problem, but I don't understand what he tries to explain as his solution ( WPF: DataGridCell override the row style color )

-- update: setting the HorizontalContentAlignment does not help completely, it results in this (the alignment of right is gone somehow):

保持左对齐

Change your TargetType and your Setter Property to the examples in your modified code below :

<Window x:Class="ColorColumnAlignment.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid ItemsSource="{Binding Path=MyData}">
            <DataGrid.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Setters>
                        <Setter Property="TextAlignment" Value="Right" />
                        <Setter Property="Background" Value="Chocolate" />
                    </Style.Setters>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
    </Grid>
</Window>

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