使用Datagrids的反应性UI

时间:2014-01-17 15:13:49

标签: wpf datagrid reactive-programming

目前,我的RadDatagrid1对RadDatagrid1有一个Cell_click操作,当选择ClientName时,该客户端信息将在DataGrid2中投影。 鼠标双击中的代码:

private void Cell_click(object sender, GridViewSelectedCellsChangedEventArgs e)
{
    Var Details = (from info in contacts
                  where info.ClientName = sender.CurrentCell.ToString()
                  select new {info.ClientName, info.ClientAddress, Info.ClientNumber});
    DataGrid2.ItemsSource = Details.ToList();
}

这是我现在所拥有的,但它应该是一个反应性UI。 在GridViewModel中,我被告知要查看的反应UI的一个例子是:

this.WhenAny(x => x.Forename, x => x.Surname, x => x.City, (p1, p2, p3) => Unit.Default).Subscribe(x => Filter());

但这对我来说并不合理。如果我能获得指导和提示如何将其转换为反应式UI请。

1 个答案:

答案 0 :(得分:0)

我是Reactive UI的新手,由于缺乏文档,我到目前为止的经验一直是经过反复试验。所以我的方法可能不正确。

确保您有支持WPF控件的ViewModel(请参阅this page

您的ViewModel应如下所示:

public class ViewModel : ReactiveObject {
    // ClientInfo type is the type of object you want to bind to the DataGrid
    private ClientInfo clientInfo;  

    public ClientInfo ClientInfo {
        set { this.RaiseAndSetIfChanged(ref clientInfo, value); }
        get { return clientInfo; }
    }

    // Move contacts IEnumerable/IQueryable to your ViewModel
    private IQueryable<ClientInfo> contacts;

    public LoadInfo(string clientName) {
        ClientInfo = (from info in contacts
                  where info.ClientName = clientName
                  select new {info.ClientName, info.ClientAddress, Info.ClientNumber})
    }    
}

确保您的View(控件类)实现IViewFor<T>,其中T是您的视图模型的类型。根据文档here绑定视图。

为您的视图执行以下操作:

// Implement the methods on that interface, which I will not include below
public partial class View : IViewFor<ViewModel> { 

    private ICommand loadClientInfo;

    public View() { // constructor
        InitializeComponent(); // Don't forget this

        // Binds the data in your ViewModel with the ItemSource of your DataGrid
        this.OneWayBind(ViewModel, vm => vm.ClientInfo, x => x.DataGrid2.ItemSource);
    }

    private void Cell_click(object sender, GridViewSelectedCellsChangedEventArgs e)
    {
        ViewModel.LoadInfo(sender.CurrentCell.ToString());
    }
}