Wpf 将前台设置为DataGrid的奇怪问题
老实说,我不确定我在这里出了什么问题,但不管出于什么原因,下面看到的前景样式没有应用到datagrid。我对此感到困惑,因为我真的不知道如何调试xamlWpf 将前台设置为DataGrid的奇怪问题,wpf,xaml,data-binding,wpfdatagrid,Wpf,Xaml,Data Binding,Wpfdatagrid,老实说,我不确定我在这里出了什么问题,但不管出于什么原因,下面看到的前景样式没有应用到datagrid。我对此感到困惑,因为我真的不知道如何调试xaml <DataGrid Name="dgProperties" Background="#1E918D8D" SelectionMode="Extended" SelectionUnit="FullRow" ItemsSource="{Binding CurFieldData}" AutoGenerateColumns="False" Can
<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:
代码:
公共部分类主窗口:窗口
{
私有ObservableCollection项=新ObservableCollection();
公共主窗口()
{
初始化组件();
对于(int i=0;i<1000;i++)
{
添加(新的GridItem{Name=“StackOverflow”+i,LeftValue=i%4,RightValue=i%2});
}
}
公共可观测收集项目
{
获取{返回项;}
设置{items=value;}
}
}
公共类GridItem
{
公共字符串名称{get;set;}
公共int LeftValue{get;set;}
public int RightValue{get;set;}
公共扩散状态扩散状态
{
得到
{
if(LeftValue==RightValue)
{
返回状态。相同;
}
返回状态不同;
}
}
}
公共枚举状态
{
不同的,
同样的
}
结果:
DataGridRow的样式可能会被对象图上方出现的设置所混淆,尤其是交替行背景。如sa_ddam213的回答所示,Xaml在没有声明其他内容的情况下工作。因此,纯粹作为诊断加速器,将这四行添加到DataGrid声明中
RowBackground="Transparent"
Background="Transparent"
AlternatingRowBackground="Transparent"
Foreground="Transparent"
然后检查前台属性及其触发器。然后,您可以在DataGrid上对这四个属性进行变异,以识别混淆影响(然后将其删除)。我复制粘贴了您的Xaml并应用了一些项目,文本为蓝色?所以它似乎在工作,你确定没有一个新的dataGrid样式覆盖前台属性吗?你更改了什么来修复它?@GarryVass,我没有更改任何内容。他的代码按预期工作,我只发布了我的测试代码,以防他想独立于应用程序进行测试,因为这可能会帮助他发现问题。啊!知道了!我将以同样的方式添加另一个。
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
}
RowBackground="Transparent"
Background="Transparent"
AlternatingRowBackground="Transparent"
Foreground="Transparent"