【问题标题】:Weird issues styling a foreground to a DataGrid奇怪的问题为 DataGrid 设置前景样式
【发布时间】:2013-07-10 09:24:26
【问题描述】:

老实说,我不确定我在哪里出错了,但无论出于何种原因,下面看到的前景样式都没有应用于数据网格。我对此感到不知所措,因为我真的不知道如何调试 xaml。

<DataGrid Name="dgProperties" Background="#1E918D8D" SelectionMode="Extended" SelectionUnit="FullRow" ItemsSource="{Binding CurFieldData}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserSortColumns="True" IsReadOnly="True">

    <DataGrid.RowStyle>
        <Style  TargetType="{x:Type DataGridRow}" >
            <Setter Property="Foreground" Value="#3535bb" />

            <!--<Style.Triggers>
                <DataTrigger Binding="{Binding Path=DiffState}" Value="Different">
                    <Setter Property="Foreground" Value="#3535BB" />
                </DataTrigger>
            </Style.Triggers>-->

        </Style>
    </DataGrid.RowStyle>

    <DataGrid.Columns>
        <DataGridTextColumn Header="Property" FontSize="12"  Binding="{Binding Name}" Width="2*" />
        <DataGridTextColumn Header="Left Value" FontSize="12" Binding="{Binding LeftValue}"  Width="4*" />
        <DataGridTextColumn Header="Right Value" FontSize="12" Binding="{Binding RightValue}"  Width="4*"/>                                
    </DataGrid.Columns>
</DataGrid>

您可能可以通过注释掉的触发器来判断,但我最初计划重新着色网格中标记为不同的所有条目(后面代码中的枚举)。但是,这对我不起作用,所以我想尝试看看是否设置了样式,无论触发器如何。

有人看到或知道为什么没有应用这种样式吗?

【问题讨论】:

  • 我复制粘贴了您的 Xaml 并应用了一些项目,但文本是蓝色的?所以它似乎正在工作,你确定没有覆盖 Foreground 属性的另一种 dataGrid 样式

标签: wpf xaml data-binding wpfdatagrid


【解决方案1】:

它似乎工作得很好,我在下面添加了我的测试代码以防它有帮助

Xaml:

<Window x:Class="WpfApplication7.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" Name="UI">
    <DataGrid Name="dgProperties" Background="#1E918D8D" SelectionMode="Extended" SelectionUnit="FullRow" ItemsSource="{Binding Items, ElementName=UI}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserSortColumns="True" IsReadOnly="True">
        <DataGrid.RowStyle>
            <Style  TargetType="{x:Type DataGridRow}" >
                <Setter Property="Foreground" Value="Black" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=DiffState}" Value="Different">
                        <Setter Property="Foreground" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>

        <DataGrid.Columns>
            <DataGridTextColumn Header="Property" FontSize="12"  Binding="{Binding Name}" Width="2*" />
            <DataGridTextColumn Header="Left Value" FontSize="12" Binding="{Binding LeftValue}"  Width="4*" />
            <DataGridTextColumn Header="Right Value" FontSize="12" Binding="{Binding RightValue}"  Width="4*"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>

代码:

public partial class MainWindow : Window
{
    private ObservableCollection<GridItem> items = new ObservableCollection<GridItem>();

    public MainWindow()
    {
        InitializeComponent();
        for (int i = 0; i < 1000; i++)
        {
            Items.Add(new GridItem { Name = "StackOverflow" + i, LeftValue = i % 4, RightValue = i % 2 });
        }
    }

    public ObservableCollection<GridItem> Items
    {
        get { return items; }
        set { items = value; }
    }

}

public class GridItem
{
    public string Name { get; set; }

    public int LeftValue { get; set; }
    public int RightValue { get; set; }

    public DiffState DiffState
    {
        get
        {
            if (LeftValue == RightValue)
            {
                return DiffState.Same;
            }
            return DiffState.Different;
        }
    }
}

public enum DiffState
{
    Different,
    Same
}

结果:

【讨论】:

  • 你做了什么改变来修复它?
  • @GarryVass,我什么也没做。他的代码按预期工作,我只发布了我的测试代码,以防他想独立于他的应用程序进行测试,因为这可能会帮助他找到问题。
  • 啊!知道了!我会以同样的方式添加另一个。
【解决方案2】:

DataGridRow 的样式可能会被对象图上方出现的设置混淆,尤其是交替行背景。如 sa_ddam213 的回答所示,Xaml 在没有声明任何其他内容时工作。所以纯粹作为诊断加速器,将这四行添加到 DataGrid 声明中...

  RowBackground="Transparent"
  Background="Transparent"
  AlternatingRowBackground="Transparent"
  Foreground="Transparent"

然后检查您的前台属性及其触发器。然后,您可以改变 DataGrid 上的这四个属性以识别混杂影响(然后将其删除)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多