我认为这样做的最佳方法是仅使用MainWindow.xaml,而无需在该文件中的C#中编写代码。 因此,我打开了一个名为HeaderGridView.cs的新类,并尝试从那里进行绑定...我不知道我在做什么错
我没有错误,这个绑定什么也没有...
以及使用过滤器的最佳实践是什么(我的想法是在HeaderGridView.cs中创建一个方法,并在每次值更改时通过操作将其附加到每个TextBox和DatePicker) 我该怎么办?
我尝试使用此问题...但没有帮助
Bind object properties to a datagrid in WPF
Binding DataTable to DataGrid. WPF MVVM
我可以使用这种方式,并且可以正常工作。但我想避免写代码 MainWindow.xaml.cs
How Bind DataTable to DataGrid
MainWindow.xaml
<Window x:Class="ReportGenerator.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ReportGenerator"
xmlns:model="clr-namespace:MilBatDBModels;assembly=MilBatDBModels"
xmlns:db="clr-namespace:DataBaseManager;assembly=DataBaseManager"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
<Label>Report :</Label>
<TextBox MinWidth="50" ></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
<Label>Catalog :</Label>
<TextBox MinWidth="50"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
<Label> Batch :</Label>
<TextBox MinWidth="50" ></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
<Label >StartDate :</Label>
<DatePicker ></DatePicker>
<Label>EndDate :</Label>
<DatePicker></DatePicker>
</StackPanel>
<DataGrid x:Name="HeaderDataGrid" IsReadOnly="True" SelectionMode="Single" ItemsSource="{Binding HeaderGridView.HeaderTable}">
</DataGrid>
</StackPanel>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FastMember;
using MilBatDBModels;
using static DataBaseManager.PulserBaseManager;
namespace ReportGenerator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
HeaderGridView.cs
using FastMember;using MilBatDBModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using static DataBaseManager.PulserBaseManager;
namespace ReportGenerator
{
public class HeaderGridView
{
public DataTable HeaderTable { get; set; }
public HeaderGridView() {
HeaderTable = new System.Data.DataTable();
List<HeaderResult> headerList = PulserBaseManagerInstance.GetHeaderResults();
using (var reader = ObjectReader.Create(headerList))
{
HeaderTable.Load(reader);
}
//HeaderDataGrid.DataContext = headerTable;
}
public void RunFilterAction(object sender, SelectionChangedEventArgs e)
{
}
}
}
答案 0 :(得分:1)
在MainWindow方法中,您需要设置Window的数据上下文,如下所示:
<script src="https://d3js.org/d3.v5.min.js"></script>
并如下更改数据网格标记绑定:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new HeaderGridView();
}
}
通过这种方式,您正在使用MVVM模式,该模式通过使用viewmodel将模型从视图中分离出来。命名约定应如下:ItemsSource="{Binding HeaderTable}
https://intellitect.com/getting-started-model-view-viewmodel-mvvm-pattern-using-windows-presentation-framework-wpf/
可以使用以下标记设置Windows数据上下文:
HeaderGridViewModel