2013-11-21 48 views
1

創建具有未知類型的列我有2周的ViewModels獲取數據網格使用反射

Case1ViewModel : ViewModelBase, ITestInterface 
Case2ViewModel : ViewModelBase, ITestInterface 

他們都從ITestInterface繼承裏面是空的

然後我有一個

ObservableCollection<ITestInterface> 

然後我有一個DataGrid我想綁定到ViewModels

在運行時它將是ei然後我想要datagrid自動生成將取決於列表中哪個ViewModel不同的列(一次只有一種類型的ViewModel),atm它只是空的(因爲它只是看着界面),但我認爲有可能通過某種反思來做到這一點?

我綁定到一個類的

public ObservableCollection<ITestInterface> Transactions 
    { 
     get { return _transactions; } 
     set 
     { 
      _transactions = value; 
      OnPropertyChanged(() => Transactions); 
     } 
    } 

例如,在列表中的集合 公共類WholesaleDialogTradeItemViewModel:ViewModelBase,ITestInterface { 私人字符串_tradeDirection; private string _book; 私人字符串_counterpart; private int _volume; private int _price; 私人字符串_product; 私人字符串_delivery; 私人字符串_period;

public string TradeDirection 
    { 
     get { return _tradeDirection; } 
     set 
     { 
      _tradeDirection = value; 
      OnPropertyChanged(() => TradeDirection); 
     } 
    } 

    public string Book 
    { 
     get { return _book; } 
     set 
     { 
      _book = value; 
      OnPropertyChanged(() => Book); 
     } 
    } 

    public string Counterpart 
    { 
     get { return _counterpart; } 
     set 
     { 
      _counterpart = value; 
      OnPropertyChanged(() => Counterpart); 
     } 
    } 

    public string Product 
    { 
     get { return _product; } 
     set { _product = value; OnPropertyChanged(() => Product); } 
    } 

    public string Delivery 
    { 
     get { return _delivery; } 
     set { _delivery = value; OnPropertyChanged(()=> Delivery);} 
    } 

    public string Period 
    { 
     get { return _period; } 
     set { _period = value; OnPropertyChanged(() => Period);} 
    } 

    public int Volume 
    { 
     get { return _volume; } 
     set { _volume = value; OnPropertyChanged(() => Volume);} 
    } 

    public int Price 
    { 
     get { return _price; } 
     set { _price = value; OnPropertyChanged(() => Price);} 
    } 
} 

public interface ITestInterface 
{ 
    string Counterpart { get; } 
} 

的XAML

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition /> 
     <RowDefinition /> 
    </Grid.RowDefinitions> 
    <DataGrid 
     Grid.Row="0" 
     Width="600" 
     SelectionMode="Extended" 
     SelectionUnit="FullRow" 
     CanUserDeleteRows="False" 
     CanUserResizeRows="False" 
     IsReadOnly="True" 
     AutoGenerateColumns="True" 
     AutoGeneratingColumn="DataGrid_OnAutoGeneratingColumn" 
     ItemsSource="{Binding Transactions, Mode=OneWay}"> 

    </DataGrid> 
    <Grid 
     Grid.Row="1"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Button 
      Grid.Column="0" 
      Margin="5,10,5,0" 
      cal:Click.Command="{Binding SaveCommand}" 
      Height="25"> 
      <AccessText>_OK</AccessText> 
     </Button> 
     <Button 
      Grid.Column="1" 
      Margin="5,10,5,0" 
      cal:Click.Command="{Binding CancelCommand}" 
      Height="25"> 
      <AccessText>_Cancel</AccessText> 
     </Button> 
    </Grid> 
</Grid> 

在這個例子的結果是我得到的對口字符串,而是從WholesaleDialogTradeItemViewModel沒有顯示,如果我從接口沒有刪除對口所有其他屬性如圖所示

另一示例(右鍵點擊以顯示全幅)enter image description here

+0

發佈您的XAML。 – sthotakura

+0

我已經刪除了我的答案,因爲它不回答yoru問題。我會看看我能否找到更有用的東西... – oerkelens

回答

0

答案結束了與

public ObservableCollection<object> Transactions 

更換

public ObservableCollection<ITestInterface> Transactions 

顯然迫使編譯器使用反射來找出顯示什麼,如果我使用的接口它只是採取