gpt4 book ai didi

c# - 带有摘要行的 MVVM DataGrid

转载 作者:行者123 更新时间:2023-12-03 11:01:57 25 4
gpt4 key购买 nike

我一直在努力进行简单的网格设置,但在 DataGrid 上方或下方获取摘要行时遇到问题。他们是否有某种方法可以获取特定列的数据网格中的总计数?

这是我所拥有的:

<Window xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"  
x:Class="TestWindow"
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"
mc:Ignorable="d"
Title="Test Window" Height="645" Width="900">
<StackPanel Orientation="Vertical">
<DataGrid Height="500" ItemsSource="{Binding GridDetails}" HorizontalAlignment="Left" VerticalAlignment="Top" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="Name" Header="Name" Binding="{Binding Path='Name', Mode=OneTime}" IsReadOnly="True" Width="200" />
<DataGridTextColumn x:Name="ID" Header="ID" Binding="{Binding Path='ID', Mode=OneTime}" IsReadOnly="True" Width="100">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="NumClaims" Header="Claims" Binding="{Binding Path='NumClaims', Mode=OneTime}" IsReadOnly="True" Width="100">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="NewClaims" Header="New Claims" Binding="{Binding Path='NumNewClaims', Mode=OneTime}" IsReadOnly="True" Width="80">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{Binding NumNewClaims}"/>
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Window>

在 View 模型中

private ObservableCollection<GridDetail> _gridDetails;
public ObservableCollection<GridDetail> GridDetails
{
get { return _gridDetails; }
set { SetProperty(ref _gridDetails, value); }
}

private int _totalClaims;
public int TotalClaims
{
get { return _totalClaims; }
set { SetProperty(ref _totalClaims, value); }
}

private int _totalNewClaims;
public int TotalNewClaims
{
get { return _totalNewClaims; }
set { SetProperty(ref _totalNewClaims, value); }
}

我遇到的麻烦是弄清楚如何将我一直在跟踪的总计放在列标题中的 View 模型中。这是我的首选方法,但如果实现起来更简单,我也可以使用卡住在网格顶部的摘要行。

任何建议将不胜感激。

最佳答案

您想为每列创建一个自定义列标题。然后,将 TextBlock 与 ViewModel 中的属性绑定(bind)。但是,您需要使用 DataContext 才能使其工作。让我们一步一步来。

自定义标题将是这样的:

<DataGridTextColumn x:Name="NumClaims" Binding="{Binding Path='NumClaims', Mode=OneTime}" IsReadOnly="True" Width="100">

<DataGridTextColumn.Header>
<TextBlock Text="Claims" />
</DataGridTextColumn.Header>

<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

现在这将与您现在的相同,但现在您可以在标题中添加更多内容,例如 StackPanel .
<DataGridTextColumn.Header>
<StackPanel>
<TextBlock Text="Claims" />
<TextBlock Text="35"/> <!-- Dummy value -->
</StackPanel>
</DataGridTextColumn.Header>

如果,就像你的情况一样,你想从你的 ViewModel 绑定(bind)一个属性,你会写
<DataGridTextColumn.Header>
<StackPanel>
<TextBlock Text="Claims" />
<TextBlock DataContext="{Binding RelativeSource={RelativeSource AncestorType=DataGrid, Mode=FindAncestor}, Path=DataContext}" Text="{Binding TotalClaims, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</DataGridTextColumn.Header>

我希望这个对你有用。

关于c# - 带有摘要行的 MVVM DataGrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57515260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com