gpt4 book ai didi

c# - 数据网格列标题按钮

转载 作者:行者123 更新时间:2023-12-03 10:22:27 26 4
gpt4 key购买 nike

我在数据网格列的标题中有一个按钮,但它没有绑定(bind)到 View 模型上的属性。

这是 XAML:

    <DataGrid ItemsSource="{Binding BillingHistoryList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ar_bill_dt}">
<DataGridTextColumn.Header>
<Button Content="Bill Date" Command="{Binding SortData}" />
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Amount" Binding="{Binding ar_orig_amt}" />
<DataGridTextColumn Header="Running Total" Binding="{Binding RunningTotal}" />
</DataGrid.Columns>
</DataGrid>

数据显示在网格中,但是当我按下按钮时它不会执行。输出中的错误是:

System.Windows.Data Error: 40 : BindingExpression path error: 'SortData' property not found on 'object' ''Button' (Name='')'. BindingExpression:Path=SortData; DataItem='Button' (Name=''); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')



如果我将按钮移到数据网格之外,它就可以工作。我猜问题出在按钮属性的路径上,但我不知道如何指定它才能正常工作。

我需要做什么才能让按钮工作?

这是 View 模型的相关部分:
public class BillingHistoryViewModel: ValidatingBindableBase, IRegionManagerAware, INavigationAware
{
public IRegionManager RegionManager { get; set; }

private ICustomerService _service;
private ITransactionHistoryService _trnsService;
private string _Title;
public string Title { get => _Title; set => SetProperty(ref _Title, value); }

public BillingHistoryViewModel(ICustomerService service, ITransactionHistoryService trnsService)
{
Title = "Billing History";
_service = service;
_trnsService = trnsService;
SortData = new DelegateCommand(ExecuteSortData);
}

private void ExecuteSortData()
{
}


void GetBillingHistory(int account)
{
var CustId = _trnsService.GetCustId(account);

var BillHistList = new List<BillHist>();

var BillHistoryEntries = _trnsService.GetEntriesByCustId(CustId);

decimal runningTotal = 0.0m;

foreach(var entry in BillHistoryEntries)
{
var newEntry = new BillHist(entry);

newEntry.RunningTotal = runningTotal;
runningTotal += newEntry.ar_orig_amt.Value;

BillHistList.Add(newEntry);
}

BillHistList.Add(new BillHist() {ar_bill_dt=DateTime.Now, ar_orig_amt=10.0m, RunningTotal = 100.0m });
BillingHistoryList = new ListCollectionView(BillHistList);
}


private ICollectionView _BillingHistoryList;
public ICollectionView BillingHistoryList { get => _BillingHistoryList; set => SetProperty(ref _BillingHistoryList, value); }

public DelegateCommand SortData { get; set; }
}

当我在 ExecuteSort 上设置断点时,它永远不会到达那里。

最佳答案

在那DataGridTextColumn ,DataContext 将成为计费历史记录项(无论 BillingHistoryList 中的什么),而不是计费历史记录项集合的所有者。这就是为什么{Binding ar_bill_dt}作品:ar_bill_dt肯定是计费历史项目的属性。

如果 SortData是拥有 BillingHistoryList 的同一 View 模型的属性, 试试这个:

<DataGrid ItemsSource="{Binding BillingHistoryList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ar_bill_dt}">
<DataGridTextColumn.Header>
<Button
Content="Bill Date"
Command="{Binding DataContext.SortData, RelativeSource={RelativeSource AncestorType=DataGrid}}"
/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Amount" Binding="{Binding ar_orig_amt}" />
<DataGridTextColumn Header="Running Total" Binding="{Binding RunningTotal}" />
</DataGrid.Columns>
</DataGrid>

另一个版本。无论如何,这是一种更受人尊敬的方式:
<DataGridTextColumn Binding="{Binding ar_bill_dt}">
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<Button
Content="Bill Date"
Command="{Binding DataContext.SortData, RelativeSource={RelativeSource AncestorType=DataGrid}}"
/>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>

关于c# - 数据网格列标题按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209997/

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