简体   繁体   English

如何在Silverlight DataGrid中的编辑模式下更改单元格的文本颜色?

[英]How to change text color of cell in edit mode in Silverlight DataGrid?

In Silverlight 4 I have a DataGrid with a DataGridTextColumn . 在Silverlight 4中我有一个DataGridDataGridTextColumn The cells in this column are editable. 此列中的单元格是可编辑的。 Now I have the problem that I changed the color of the whole grid to white on black instead of the default black on white. 现在,我有一个问题,我将整个网格的颜色更改为黑底白字,而不是默认的黑底白字。 I managed to change all relevant colors, except for the text color of the cell during edit mode . 我设法更改了所有相关的颜色,除了在编辑模式下单元格文本颜色 This is still black, so it looks pretty bad. 它仍然是黑色的,因此看起来很糟糕。

Any hints how to do it? 有什么提示怎么做吗? I've now spent plenty of time googling and searching for this problem but found no solution. 我现在花了很多时间在Google搜索和搜索此问题上,但是没有找到解决方案。 Thanks in advance! 提前致谢!

Try changing the background of the DataGridCell, the TextBox background in the DataGridTextColumn is transparent, so it should pickup the cell's background. 尝试更改DataGridCell的背景,DataGridTextColumn中的TextBox背景是透明的,因此它应该拾取单元格的背景。

Here is something that worked for me: 这对我有用:

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
         x:Class="DataGridTextBoxBackground.MainPage"
         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:p="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>
        <Style TargetType="p:DataGridCell">
            <Setter Property="Background" Value="Black" />
            <Setter Property="Foreground" Value="White" />
        </Style>
    </Grid.Resources>
    <sdk:DataGrid x:Name="gridItems" AutoGenerateColumns="False">
        <sdk:DataGrid.Columns>
            <sdk:DataGridTextColumn Binding="{Binding Name}" Header="Name" />
        </sdk:DataGrid.Columns>
    </sdk:DataGrid>
</Grid>

Here's the codebehind for the xaml above: 这是上面xaml的代码背后:

    namespace DataGridTextBoxBackground
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            List<DataItem> items = new List<DataItem>();
            items.Add(new DataItem("item 1"));
            items.Add(new DataItem("item 2"));
            items.Add(new DataItem("item 3"));
            items.Add(new DataItem("item 4"));
            items.Add(new DataItem("item 5"));

            this.gridItems.ItemsSource = items;
        }
    }

    public class DataItem
    {
        public DataItem() { }

        public DataItem(string name)
        {
            Name = name;
        }
        public string Name { get; set; }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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