老实说,我不确定我在哪里出错,但无论出于何种原因,下面看到的前景风格都没有应用到数据网格。我不知道这个,因为我真的不知道如何调试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>
您可以通过注释掉的触发器来判断,但我最初计划重新着色网格中标记为不同的所有条目(后面的代码中的枚举)。然而,这对我不起作用,所以我想尝试看看风格是否被设置,无论触发器如何。
有人看到或知道为什么没有应用这种风格?
答案 0 :(得分:0)
它似乎工作得很好,我已经添加了我的测试代码,因为它有帮助
的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
}
结果:
答案 1 :(得分:0)
DataGridRow的样式可能会被对象图上方出现的设置混淆,尤其是交替行背景。如sa_ddam213的答案所示,Xaml在没有声明任何其他内容时工作。纯粹作为诊断加速器,将这四行添加到DataGrid声明......
RowBackground="Transparent"
Background="Transparent"
AlternatingRowBackground="Transparent"
Foreground="Transparent"
然后检查您的前景属性及其触发器。然后,您可以在DataGrid上改变这四个属性,以识别混杂的影响(然后将其删除)。