2016-12-27 84 views
1

這是一個示例代碼:如何更改DataGrid ItemsSource更改屬性時?

public class GridViewWindowViewModel : INotifyPropertyChanged 
    { 
     public GridViewWindowViewModel() 
     { 
      Tables = new ObservableCollection<string> { "Person", "Car" }; 
      SainaAccessEntity = new SainaAccessEntity { TableName = "Person" }; 
     } 

     private SainaAccessEntity _SainaAccessEntity; 

     public SainaAccessEntity SainaAccessEntity 
     { 
      get { return _SainaAccessEntity; } 
      set 
      { 
       if (_SainaAccessEntity != value) 
       { 
        _SainaAccessEntity = value; 
        OnPropertyChanged(); 
       } 
      } 
     } 

     private ObservableCollection<string> _Tables; 

     public ObservableCollection<string> Tables 
     { 
      get { return _Tables; } 
      set 
      { 
       if (_Tables != value) 
       { 
        _Tables = value; 
        OnPropertyChanged(); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
     private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

    } 

這是模型類:

public class SainaAccessEntity : INotifyPropertyChanged 
    { 
     private string _TableName; 

     public string TableName 
     { 
      get { return _TableName; } 
      set 
      { 
       if (value != _TableName) 
       { 
        _TableName = value; 
        OnPropertyChanged(); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
     private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

自定義數據網格視圖:

public class MyDataGrid : DataGrid 
{ 
    public MyDataGrid() 
    { 
     var x = new ObservableCollection<Person> { new Person { Name = "Ali", Family = "Jalilvand" } }; 
    } 
    public SainaAccessEntity SainaAccessEntity 
    { 
     get { return (SainaAccessEntity)GetValue(SainaAccessEntityProperty); } 
     set { SetValue(SainaAccessEntityProperty, value); } 
    } 
    public static readonly DependencyProperty SainaAccessEntityProperty = 
     DependencyProperty.Register("SainaAccessEntity", typeof(SainaAccessEntity), typeof(MyDataGrid), new PropertyMetadata(null, SainaAccessEntity_Changed)); 

    private static void SainaAccessEntity_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var u = d as MyDataGrid; 
     if (u.SainaAccessEntity.TableName == "Person") 
     { 
      u.ItemsSource = new ObservableCollection<Person> { new Person { Name = "Ali", Family = "Bayat" } }; 
     } 
     else 
     { 
      u.ItemsSource = new ObservableCollection<Car1> { new Car1 { Name = "BMW", Model = "518",Color="Red" } }; 
     } 

    } 

} 

汽車和人物型號:

public class Car1 
{ 
    public string Model { get; set; } 
    public string Name { get; set; } 
    public string Color { get; set; } 

} 

public class Person 
{ 
    public string Name { get; set; } 
    public string Family { get; set; } 
} 

主窗口XAML:

<Window x:Class="TestWpfApplication.GridViewWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:TestWpfApplication" 
     mc:Ignorable="d" 
     Title="GridViewWindow" Height="300" Width="300"> 
    <Window.Resources> 
     <local:GridViewWindowViewModel x:Key="Vm"/> 
    </Window.Resources> 
    <Grid DataContext="{StaticResource Vm}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="auto"/> 
     </Grid.RowDefinitions> 
     <local:MyDataGrid VerticalAlignment="Top" SainaAccessEntity="{Binding SainaAccessEntity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
     <ComboBox Margin="0,5.694,0,0" Grid.Row="1" ItemsSource="{Binding Tables, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SainaAccessEntity.TableName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></ComboBox> 
    </Grid> 
</Window> 

如何在更改DataGrid ItemsSource時更改屬性?我有自定義DataGrid在WPF中,它有一個屬性,定義表名稱,我想動態更改ItamsSource,當更改此屬性...

回答

2

自定義DataGrid的SainaAccessEntity屬性綁定到視圖模型的SainaAccessEntity財產,但你從來沒有設置該屬性時在ComboBox變化的選擇。

視圖模型的Tables屬性應返回一個ObservableCollection <SainaAccessEntity>,而不是一個ObservableCollection的<串>:

public class GridViewWindowViewModel : INotifyPropertyChanged 
{ 
    public GridViewWindowViewModel() 
    { 
     Tables = new ObservableCollection<SainaAccessEntity> 
      { 
       new SainaAccessEntity { TableName = "Person" }, 
       new SainaAccessEntity { TableName = "Car" }, 
      }; 
     SainaAccessEntity = Tables[0]; 
    } 

    private SainaAccessEntity _SainaAccessEntity; 

    public SainaAccessEntity SainaAccessEntity 
    { 
     get { return _SainaAccessEntity; } 
     set 
     { 
      if (_SainaAccessEntity != value) 
      { 
       _SainaAccessEntity = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    private ObservableCollection<SainaAccessEntity> _Tables; 
    public ObservableCollection<SainaAccessEntity> Tables 
    { 
     get { return _Tables; } 
     set 
     { 
      if (_Tables != value) 
      { 
       _Tables = value; 
       OnPropertyChanged(); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 
    private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

您應該然後ComboBox的SelectedItem屬性綁定到視圖的SainaAccessEntity財產模型並將組合框的DisplayMemberPath屬性設置爲「TableName」:

<ComboBox Margin="0,5.694,0,0" Grid.Row="1" ItemsSource="{Binding Tables, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        SelectedItem="{Binding SainaAccessEntity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        DisplayMemberPath="TableName"></ComboBox> 

用這些cha如果在ComboBox中選擇一個項目,DataGrid的ItemsSource應該按預期進行更改。

1

在這裏您的屬性'SainaAccessEntity'不更改,只有TableName正在更改。爲此,您可以嘗試下面的代碼,或者您可以將「SainaAccessEntity」中的類型更改爲字符串。

變化「SainaAccessEntity_Changed」方法的代碼

 var u = d as MyDataGrid; 

     if (u.SainaAccessEntity == null) 
     { 
      return; 
     } 

     // intial set value 
     if (u.SainaAccessEntity.TableName == "Person") 
     { 
      u.ItemsSource = new ObservableCollection<Person> { new Person { Name = "Ali", Family = "Bayat" } }; 
     } 
     else 
     { 
      u.ItemsSource = new ObservableCollection<Car1> { new Car1 { Name = "BMW", Model = "518", Color = "Red" } }; 
     } 

     // set value on change 
     var _notify = u.SainaAccessEntity as INotifyPropertyChanged; 
     if (_notify != null) 
     { 
      _notify.PropertyChanged += (sender, ev) => 
      { 
       if (u.SainaAccessEntity.TableName == "Person") 
       { 
        u.ItemsSource = new ObservableCollection<Person> { new Person { Name = "Ali", Family = "Bayat" } }; 
       } 
       else 
       { 
        u.ItemsSource = new ObservableCollection<Car1> { new Car1 { Name = "BMW", Model = "518", Color = "Red" } }; 
       } 
      }; 
     }