我有一个简单的列表视图,其中显示了一些与按小时进行投资相对应的指标。我需要冻结最后一行并包含其值的每个总和
这是我正在使用的代码:
MainWindow.xaml:
<Window x:Class="WpfTest.MainWindow"
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:local="clr-namespace:WpfTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Window.Resources>
<CollectionViewSource x:Key="ItemCollectionViewSource" CollectionViewType="ListCollectionView"/>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding Source={StaticResource ItemCollectionViewSource}}"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListView.View>
<GridView>
<GridViewColumn Width="80" Header="id customer" DisplayMemberBinding="{Binding id}"/>
<GridViewColumn Width="60" Header="00 - 03 h" DisplayMemberBinding="{Binding qty_00}"/>
<GridViewColumn Width="60" Header="03 - 06 h" DisplayMemberBinding="{Binding qty_03}" />
<GridViewColumn Width="60" Header="06 - 09 h" DisplayMemberBinding="{Binding qty_06}" />
<GridViewColumn Width="60" Header="09 - 12 h" DisplayMemberBinding="{Binding qty_09}" />
<GridViewColumn Width="60" Header="12 - 15 h" DisplayMemberBinding="{Binding qty_12}" />
<GridViewColumn Width="60" Header="15 - 18 h" DisplayMemberBinding="{Binding qty_15}" />
<GridViewColumn Width="60" Header="18 - 21 h" DisplayMemberBinding="{Binding qty_18}" />
<GridViewColumn Width="60" Header="21 - 24 h" DisplayMemberBinding="{Binding qty_21}" />
<GridViewColumn Width="60" Header="Total by Id" DisplayMemberBinding="{Binding totalById}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
这是我的Invest.cs类
public class Invest
{
public string id { get; set; }
public decimal qty_00 { get; set; }
public decimal qty_03 { get; set; }
public decimal qty_06 { get; set; }
public decimal qty_09 { get; set; }
public decimal qty_12 { get; set; }
public decimal qty_15 { get; set; }
public decimal qty_18 { get; set; }
public decimal qty_21 { get; set; }
public decimal totalById { get; set; }
}
及其背后的代码:
public partial class MainWindow : Window
{
private List<Invest> investList;
public MainWindow()
{
InitializeComponent();
GetData();
CalculateTotalsById();
}
private void GetData()
{
investList = new List<Invest>();
investList.Add(new Invest() { id = "01", qty_00 = 14m, qty_03 = 10m, qty_06 = 12, qty_09 = 0 });
investList.Add(new Invest() { id = "02", qty_15 = 25m, qty_18 = 15m, qty_12 = 21 });
investList.Add(new Invest() { id = "03", qty_00 = 20m, qty_03 = 5m, qty_09 = 30 });
investList.Add(new Invest() { id = "04", qty_18 = 15m, qty_21 = 5m });
}
private void CalculateTotalsById()
{
foreach (Invest item in investList)
{
item.totalById = investList
.Where(x => x.id == item.id)
.Sum(x => x.qty_00 + x.qty_03 + x.qty_06 + x.qty_09 + x.qty_12 + x.qty_15 + x.qty_18 + x.qty_21)
;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CollectionViewSource itemCollectionViewSource;
itemCollectionViewSource = (CollectionViewSource)(FindResource("ItemCollectionViewSource"));
itemCollectionViewSource.Source = investList;
}
}
我目前正在获取最后一列的总计,但我需要最后一行,每一行都有总计。可以将那列冻结吗?
我知道下一步是使用任何模板,但是我完全不知道模板逻辑。
答案 0 :(得分:0)
我不完全理解你所说的冻结。
以下是DataGrid的示例(您编写了ListView或DataGrid):
请注意只读的“按ID总计”列 Xaml:
<DataGrid ItemsSource="{Binding Source={StaticResource ItemCollectionViewSource}}" AutoGenerateColumns="False"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<DataGrid.Columns>
<DataGridTemplateColumn Width="60" Header="id customer" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding id}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="00 - 03 h" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_00}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="03 - 06 h" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_03}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="06 - 09 h">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_06}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="09 - 12 h">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_09}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="12 - 15 h" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_12}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="15 - 18 h" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_15}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="18 - 21 h" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_18}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" Header="21 - 24 h">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding qty_21}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="60" IsReadOnly="True" Header="Total by Id">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding totalById}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我还更改了DTO类,以支持该行的自动递增:
public class Invest
{
public string id { get; set; }
public decimal qty_00 { get; set; }
public decimal qty_03 { get; set; }
public decimal qty_06 { get; set; }
public decimal qty_09 { get; set; }
public decimal qty_12 { get; set; }
public decimal qty_15 { get; set; }
public decimal qty_18 { get; set; }
public decimal qty_21 { get; set; }
public decimal totalById
{
get
{
return qty_00 + qty_03 + qty_06 + qty_09 + qty_12 + qty_15 + qty_18 + qty_21;
}
}
}
希望对您有所帮助:)