【问题标题】:WPF BindingSourceWPF 绑定源
【发布时间】:2013-11-07 00:40:35
【问题描述】:

通过从 WinForms 转换我的一个应用程序来学习 WPF 执行以下操作的 WPF 方式是什么

 DataTable _current = _connections.Copy();
 BindingSource _bs = new BindingSource();
 bs.DataSource = _current;
 bs.Filter = "Client = '" + _selectedClient + "'";

过滤掉新的 DataTable 表后,我需要将绑定源分配给 DataGrid。

更新 2 我添加了以下内容

 public ObservableCollection<SupportConnectionData> _supportConnections = new ObservableCollection<SupportConnectionData>();

将给定的数据表转成 ObservableCollection

 DataTable _dt = Global.RequestSupportConnections(_token);
                _dt = Crypto.DecryptDataTable(_dt);
                ObservableCollection<SupportConnectionData> _connections = new ObservableCollection<SupportConnectionData>();

                foreach (DataRow _row in _dt.Rows)
                {
                    SupportConnectionData _supportConnection = new SupportConnectionData()
                    {
                        _client = _row["Client"].ToString(),
                        _server = _row["Server"].ToString(),
                        _user = _row["User"].ToString(),
                        _connected = _row["Connected"].ToString(),
                        _disconnected = _row["Disconnected"].ToString(),
                        _reason = _row["Reason"].ToString(),
                        _caseNumber = _row["CaseNumber"].ToString()
                    };
                    _connections.Add(_supportConnection);
                }

                //let me assign new collection to bound collection
                App.Current.Dispatcher.BeginInvoke((Action)(() => { _supportConnections = _connections; }));
                //this allows it to update changes to ui
                dgSupportConnections.Dispatcher.BeginInvoke((Action)(() => { dgSupportConnections.DataContext = _supportConnections; }));

XAML

  <DataGrid x:Name="dgSupportConnections" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" AutoGenerateColumns="False" ItemsSource="{Binding}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Client" Binding="{Binding _client}"/>
                        <DataGridTextColumn Header="Server" Binding="{Binding _server}"/>
                        <DataGridTextColumn Header="User" Binding="{Binding _user}"/>
                        <DataGridTextColumn Header="Connected" Binding="{Binding _connected}"/>
                        <DataGridTextColumn Header="Disconnected" Binding="{Binding _disconnected}"/>
                        <DataGridTextColumn Header="Reason" Binding="{Binding _reason}"/>
                        <DataGridTextColumn Header="Case Number" Binding="{Binding _caseNumber}"/>
                    </DataGrid.Columns>
                </DataGrid>

【问题讨论】:

    标签: c# wpf c#-4.0


    【解决方案1】:

    将您的数据库对象(或者无论您如何获取它们)放入一个集合(例如 MyCollection 作为 Type 的 ObservableCollection)或集合视图源,然后绑定到该集合。在 wpf 中,您必须使用 xaml 视图绑定到的类的上下文。因此,如果数据网格的直接上下文是后面的代码,那么您可以将此行添加到数据网格以绑定到集合:

    <DataGrid ItemsSource="{Binding MyCollection}" / >
    

    在 win 表单中,您可以在代码中将集合分配给 datagirid,但在 WPF 中,您在 xaml 中声明绑定,而“WPF 引擎”会负责其余的工作。有一点学习曲线,但它非常灵活,在我看来可以减少代码。

    但这引发了关于应用程序架构的更大讨论。我建议查看 MVVM 以在模型(您的数据)、视图(用户界面)和 ViewModel(处理您的业务逻辑)之间创建解耦或关注点分离。这将使您的应用程序更易于维护和测试。

    编辑 1:示例

    窗口的xaml:

    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
        <Grid>
            <DataGrid x:Name="MyDataGrid" AutoGenerateColumns="True" ItemsSource="{Binding MyObjectCollection}" DataContext="{Binding}"  />
        </Grid>
    </Window>
    

    Xaml 窗口背后的代码:

    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Collections.ObjectModel;
    
    class MainWindow
    {
    
    
        public ObservableCollection<MyObject> MyObjectCollection = new ObservableCollection<MyObject>();
    
        public MainWindow()
        {
            // This call is required by the designer.
            InitializeComponent();
    
            // Add any initialization after the InitializeComponent() call.
            for (i = 1; i <= 10; i++) {
                MyObject newObject = new MyObject {
                    age = i,
                    name = "Full Name"
                };
                MyObjectCollection.Add(newObject);
            }
    
    
            MyDataGrid.ItemsSource = MyObjectCollection;
        }
    }
    
    public class MyObject
    {
        public string name { get; set; }
        public string age { get; set; }
    }
    

    虽然此示例适用于学习,但我不建议将此方法用于生产应用程序。我认为您需要研究 MVVM 或 MVC 才能拥有可维护和可测试的应用程序。

    【讨论】:

    • 是的,基本上涵盖了它:)
    • 用我正在尝试的内容更新了原始帖子
    • 你能发布你的窗口/页面的数据上下文或任何保存你的数据网格的东西
    • 更新了控件的完整 xaml。这就是你要找的吗?
    • 标签项是页面中的顶级对象吗?你如何定义你的代码背后?当您运行应用程序时,输出窗口中是否有任何绑定错误。这可以帮助追踪正在发生的事情
    猜你喜欢
    • 2010-10-19
    • 1970-01-01
    • 2014-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多