2014-01-17 49 views
1

當前我的RadDatagrid1對RadDatagrid1具有Cell_click操作,並且當選擇ClientName時,該客戶端信息投影到DataGrid2中。鼠標雙擊之內 碼點擊:與Datagrids的反應性用戶界面

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。 reactitve UI的一個例子,我被告知要看看這是在GridViewModel:

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

但是,這並不完全意義的我。如果我能得到指導和技巧如何將其轉換爲反應式UI請。

回答

0

我是Reactive UI的新手,由於缺少文檔,迄今爲止我的經驗一直是通過試驗和錯誤。所以我下面的方法可能不正確。

確保你有一個視圖模型支持您的WPF控件(參見this page

您的視圖模型應該是這個樣子:

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}) 
    }  
} 

確保您的視圖(控制類)實現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()); 
    } 
}