2017-04-06 66 views
1

我有一个gridcontrol与我在我的代码还没有提到WPF - 绑定一个gridcontrol所选行的数据,而在另一个gridcontrol填充它

<dxg:GridControl HorizontalAlignment="Stretch" Height="300" VerticalAlignment="Top" x:Name="grid1" AutoPopulateColumns="False" ItemsSource="{Binding Collection1}" > 
        <dxg:GridControl.View > 
         <dxg:TableView x:Name="TableView1" /> 
        </dxg:GridControl.View> 
       . 
       . 
       . 
       . 

我有相同的另一种网格控制各个领域与各个领域

<dxg:GridControl HorizontalAlignment="Stretch" Height="250" VerticalAlignment="Top" x:Name="grid2" AutoPopulateColumns="False" 
           ItemsSource="{Binding ElementName="TableView1" ,path=Collection2.FocusedRow}" > 
        <dxg:GridControl.View > 
         <dxg:TableView x:Name="TableView2" /> 
        </dxg:GridControl.View> 
       . 
       . 
       . 
       . 

现在COLLEC页检查一次1 编号是主键和collection2 colID是外键两者都具有在这里彼此

方案的关系是,如果我选择在GRID1所有相应的记录行必须显示在网格2

public class myCollection: BindingList<orders> 
{ 
    public DataContext dc; 

    public myCollection(IList<orders> list) 
     : base(list) 
    { 
    } 

    protected override void RemoveItem(int index) 
    { 
     orders deleteItem = this.Items[index]; 

     if (Dc.Order != null) 
     { 
      Dc.Order.DeleteOnSubmit(deleteItem); 
     } 
     base.RemoveItem(index); 
    } 

} 

我的订单和通用类,使用主泛型类相同

+0

是Collection2是Collection1中对象类型的属性吗? – mm8

+0

Collection2是collection1中的EntityRef对象 –

+0

它需要是一个IEnumerable,以便您能够将DataGrid的ItemsSource属性绑定到它。在实际场景中,在WPF应用程序中使用自动生成的实体框架类很少有用。你应该创建你自己的类。 – mm8

回答

1

如果我在XAML性能方面讲,在这里您要根据1st Datagrid的SelectedItem属性更新2nd Datagrid的ItemsSource属性。

要实现此目的,请在ViewModel中添加一个新属性“SelectedItemDg1”,该属性将保存第一个DataGrid的选择。在此“SelectedItemDg1”属性的Setter中,根据需要设置Collection2。

确保实现INotifyPropertyChanged接口并为这两个集合使用ObservableCollection类型。

以下为相同的代码示例:

模型类:

public class Country 
{ 
    public string CountryName { get; set; } 

    public int CountryId { get; set; } 

    public List<State> States { get; set; } 
} 

public class State 
{ 
    public string StateName { get; set; } 
    public int StateId { get; set; } 
} 

视图模型:

public class MainWindowViewModel : INotifyPropertyChanged 
{ 

    public MainWindowViewModel() 
    { 
     CountriesCollection = new ObservableCollection<Country>(); 
     StateCollection = new ObservableCollection<State>(); 
     LoadData(); 
    } 

    private ObservableCollection<Country> _CountriesCollection; 

    public ObservableCollection<Country> CountriesCollection 
    { 
     get { return _CountriesCollection; } 
     set 
     { 
      _CountriesCollection = value; 
      NotifyPropertyChanged("CountriesCollection"); 
     } 
    } 

    private ObservableCollection<State> _StatesCollection; 

    public ObservableCollection<State> StateCollection 
    { 
     get { return _StatesCollection; } 
     set 
     { 
      _StatesCollection = value; 
      NotifyPropertyChanged("StateCollection"); 
     } 
    } 

    private Country _SelectedCountry; 

    public Country SelectedCountry 
    { 
     get { return _SelectedCountry; } 
     set 
     { 
      _SelectedCountry = value; 
      if (_SelectedCountry != null && _SelectedCountry.States != null) 
      { 
       StateCollection = new ObservableCollection<State>(_SelectedCountry.States); 
      } 
      NotifyPropertyChanged("SelectedCountry"); 
     } 
    } 

    private void LoadData() 
    { 
     if (CountriesCollection != null) 
     { 
      CountriesCollection.Add(new Country 
      { 
       CountryId = 1, 
       CountryName = "India", 
       States = new List<State> 
          { 
            new State { StateId = 1, StateName = "Gujarat"}, 
            new State { StateId = 2, StateName = "Punjab"}, 
            new State { StateId = 3, StateName = "Maharastra"} 
          } 
      }); 
      CountriesCollection.Add(new Country 
      { 
       CountryId = 2, 
       CountryName = "Chine", 
       States = new List<State> 
          { 
            new State { StateId = 4, StateName = "Chine_State1"}, 
            new State { StateId = 5, StateName = "Chine_State2"}, 
            new State { StateId = 6, StateName = "Chine_State3"} 
          } 
      }); 
      CountriesCollection.Add(new Country 
      { 
       CountryId = 3, 
       CountryName = "japan", 
       States = new List<State> 
          { 
            new State { StateId = 7, StateName = "Japan_State1"}, 
            new State { StateId = 8, StateName = "Japan_State2"}, 
            new State { StateId = 9, StateName = "Japan_State3"} 
          } 
      }); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info)); 
    } 

} 

XALM:

<StackPanel Orientation="Horizontal" > 
    <DataGrid AutoGenerateColumns="True" 
       Height="300" Width="300" 
       HorizontalAlignment="Left" Margin="30" 
       ItemsSource="{Binding CountriesCollection}" 
       SelectedItem="{Binding SelectedCountry}"> 

    </DataGrid> 
    <DataGrid AutoGenerateColumns="True" 
       Height="300" Width="300" 
       HorizontalAlignment="Left" Margin="30" 
       ItemsSource="{Binding SelectedCountry.States}"> 

    </DataGrid> 
</StackPanel> 

这里我有数据网格的AutoGenerateColumns属性,但你必须改变它按您的要求。

我希望这个示例代码能让你的事情变得容易理解。

+0

我有L2S与INotifyPropertyChanged和IBindingList。是的,这就是我所需要的 –

+0

我想知道更多,因为我是新来的。 –

+0

我看到您的问题与Country和State组合框的经典示例非常相似。这是一个这样的例子的链接。我认为你可以从这里更好地理解:http://stackoverflow.com/questions/43050501/change-combobox-itemssource-based-on-another-combobox-selection –

0

我发现做这样的高手,细节上的集合绑定在一个类来包装ObservableCollection最简单和干净的方式,揭露其ListCollectionView,并绑定你ItemsSource它,就像下面(有一些额外的代码,用于简化XML序列化):

public class ViewableCollection<T> : ObservableCollection<T> 
{ 
    private ListCollectionView _View; 

    public ViewableCollection(IEnumerable<T> items) 
     : base(items) { } 

    public ViewableCollection() 
     : base() { } 

    [XmlIgnore] 
    public ListCollectionView View 
    { 
     get 
     { 
      if (_View == null) 
      { 
       _View = new ListCollectionView(this); 
       _View.CurrentChanged += new EventHandler(InnerView_CurrentChanged); 
      } 
      return _View; 
     } 
    } 

    [XmlIgnore] 
    public T CurrentItem 
    { 
     get 
     { 
      return (T)this.View.CurrentItem; 
     } 
     set 
     { 
      this.View.MoveCurrentTo(value); 
     } 
    } 

    private void InnerView_CurrentChanged(object sender, EventArgs e) 
    { 
     this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentItem")); 
    } 

    public void AddRange(IEnumerable<T> range) 
    { 
     if (range == null) 
      throw new ArgumentNullException("range"); 

     foreach (T item in range) 
     { 
      this.Items.Add(item); 
     } 

     this.OnPropertyChanged(new PropertyChangedEventArgs("Count")); 
     this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); 
     this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, (IList)range.ToList())); 
    } 

    public void ReplaceItems(IEnumerable<T> range) 
    { 
     if (range == null) 
      throw new ArgumentNullException("range"); 

     this.Items.Clear(); 
     foreach (T item in range) 
     { 
      this.Items.Add(item); 
     } 

     this.OnPropertyChanged(new PropertyChangedEventArgs("Count")); 
     this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); 
     this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 
    } 

    public void RemoveItems(IEnumerable<T> range) 
    { 

     if (range == null) 
      throw new ArgumentNullException("range"); 

     foreach (T item in range) 
     { 
      this.Items.Remove(item); 
     } 

     this.OnPropertyChanged(new PropertyChangedEventArgs("Count")); 
     this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); 
     this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, (IList)range.ToList())); 
    } 

    public void ClearAll() 
    { 
     IList old = this.Items.ToList(); 
     base.Items.Clear(); 
     this.OnPropertyChanged(new PropertyChangedEventArgs("Count")); 
     this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); 
     this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, old)); 
    } 

    public void CallCollectionChaged() 
    { 
     this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 
    } 

    // necessary for xml easy serialization using [XmlArray] attribute 
    public static implicit operator List<T>(ViewableCollection<T> o) 
    { 
     return o == null ? default(List<T>) : o.ToList(); 
    } 

    // necessary for xml easy serialization using [XmlArray] attribute 
    public static implicit operator ViewableCollection<T>(List<T> o) 
    { 
     return o == default(List<T>) || o == null ? new ViewableCollection<T>() : new ViewableCollection<T>(o); 
    } 
} 

然后在你的ViewModel(记住要实现INotifyPropertyChanged,我使用了一个NuGet包Fody.PropertyChanged自动实现它的属性):

[ImplementPropertyChanged] 
public class MyViewModel 
{ 
    public ViewableCollection<MySecondViewModel> Collection1 { get; set; } 

    public MyViewModel() 
    { 
     this.Collection1 = new ViewableCollection<MySecondViewModel>(); 
    } 
} 

[ImplementPropertyChanged] 
public class MySecondViewModel 
{ 
    public string MyColumn1 { get; set; } 
    public string MyColumn2 { get; set; } 
    public ViewableCollection<MyThirdViewModel> Collection2 { get; set; } 

    public MySecondViewModel() 
    { 
     this.Collection1 = new ViewableCollection<MyThirdViewModel>(); 
    } 
} 

[ImplementPropertyChanged] 
public class MyThirdViewModel 
{ 
    public string MyColumn1 { get; set; } 
    public string MyColumn2 { get; set; } 
} 

//... 
this.DataContext = new MyViewModel(); 

然后,保持电网同步是如此简单:

<DataGrid ItemsSource="{Binding Collection1.View, Mode=OneWay}" 
      IsSynchronizedWithCurrentItem="True" /> 
<DataGrid ItemsSource="{Binding Collection1.CurrentItem.Collection2.View, Mode=OneWay}" 
      IsSynchronizedWithCurrentItem="True" /> 

例如在当前选择的当前选择的项目绑定到Column1财产Collection2将是:

<TextBlock Text="{Binding Collection1.CurrentItem.Collection2.CurrentItem.Column1}" /> 

此外,您还可以在您的代码后面管理选择,例如:

Collection1.CurrentItem=null; 

将清除集合上的选择。

你也可以排序(和过滤以及集团)ViewableCollection从后面的代码是这样的:

Collection1.View.SortDescriptions.Add(new SortDescription("Column1",ListSortDirection.Ascending)); 

只要记住,你should't更换整个ViewableCollection它被实例化后,只需添加/删除项目(为此目的,有方法AddRangeReplaceItems用于批量添加/替换项目,而不必上升CollectionChanged事件)

+0

检查我的问题我已经更新了它的升技,我在我的集​​合中实现了Ibindnglist。 –