c# - 通过存储库模式将 WPF 控件绑定(bind)到 SQL Server

标签 c# sql-server wpf entity-framework repository-pattern

如何将 wpf 控件绑定(bind)到 SQL Server 属性。比如说一个列表框

 <ListView>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name">
                <GridViewColumn Header="Date Assigned">
                <GridViewColumn Header="Date Due">
            </GridView>
        </ListView.View>
        <!-- iterate through all the Names in the database and output under GridViewColumn Name -->
        <!-- iterate through all the DateAssigned in the database and output under GridViewColumn Date Assigned -->
        <!-- iterate through all the DateDue in the database and output under GridViewColumn Date Due -->
    </ListView>

我正在使用 Entity Framework 和存储库模式。所以我会通过 _repository.ToList(); 将所有名称调用到一个列表中;

最佳答案

试试这个:listViewName.ItemsSource = _repository.ToList();

我还会像这样简化 Xaml:

<ListBox x:Name="listViewName">
    <ListBox.Resources>
        <DataTemplate>
            <Grid Height="22" Width="Auto">
                <TextBlock Text="{Binding Name}" />
                <TextBlock  Text="{Binding DateAssigned}" />
                <TextBlock Text="{Binding DateDue}" />
            </Grid>
        </DataTemplate>
    </ListBox.Resources>
</ListBox>

{Binding 之后的文本是您从 _repository 返回的集合中项目的属性名称。

关于c# - 通过存储库模式将 WPF 控件绑定(bind)到 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2097733/

相关文章:

wpf - 在 WPF 中仅使用 XAML 将元素定位在 Canvas 内的中心(而不是左上角)

javascript - 如何获得 Kendo 组页脚模板名称的自定义名称?

c# - 二维线段与矩形的交集

sql - 删除索引或更改

WPF 数据网格绑定(bind) : add new item

c# - 使用 WPF/C# 获取有关处理器的信息

c# - SignalR Hub 上下文中没有可用的客户端

c# - 如何在将 HTML 从 abcpdf 转换为 PDF 时加载外部图像

sql - 更改号码栏

sql - TSQL 从连接到不同 ID 的表中选择与 ID 关联的最大值?