【问题标题】:Get datagrid to create columns with unknown types using reflection获取数据网格以使用反射创建具有未知类型的列
【发布时间】:2013-11-21 08:31:09
【问题描述】:

我有 2 个视图模型

Case1ViewModel : ViewModelBase, ITestInterface
Case2ViewModel : ViewModelBase, ITestInterface

它们都继承自空的 ITestInterface

然后我有一个

ObservableCollection<ITestInterface>

然后我有一个我想将数据绑定到 ViewModel 的 DataGrid

在运行时它将是 Case1ViewModel 或 Case2ViewModel ,然后我希望数据网格根据列表中的 ViewModel 自动生成不同的列(一次只有一种 ViewModel 类型),atm 它是只是空的(因为它只是查看界面),但我认为可以通过某种反射来实现它?

示例

我绑定的集合

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

列表中的类示例 公共类 WholesaleDialogTradeItemViewModel : ViewModelBase, ITestInterface { 私人字符串_tradeDirection; 私人字符串_book; 私有字符串_counterpart; 私人 int _volume; 私人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>

这个例子的结果是我得到了 Counterpart 字符串,但是 WholesaleDialogTradeItemViewModel 中的所有其他属性都没有显示出来,如果我从 Interface 中删除 Counterpart 什么都不会显示

另一个例子(右击显示完整尺寸)

【问题讨论】:

  • 也发布您的 XAML。
  • 我已经删除了我的答案,因为它没有回答你的问题。我看看能不能找到更有用的东西……

标签: c# wpf xaml mvvm


【解决方案1】:

答案以替换结束

public ObservableCollection<ITestInterface> Transactions

public ObservableCollection<object> Transactions

显然,这迫使编译器使用反射来找出要显示的内容,如果我使用接口,它就会这样做

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-12
    • 2017-10-27
    • 1970-01-01
    • 2019-03-26
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多