将数据列表绑定到多个DataGrid

时间:2015-11-16 19:53:02

标签: c# wpf mvvm data-binding datagrid

我在ViewModel中有数据绑定到DataGrid。但我需要多个数据并将其动态绑定到多个DataGrids。

    setUpLayout: function (loaded) {
        if (typeof loaded !== 'object' || !loaded.hasOwnProperty('rows')) {
            throw new Error('Invalid format for loaded paramater in setUpLayout method.');
        }

        var initial_mode = TE.Application.reqres.request('editor:mode');
        TE.Application.vent.trigger('editor:toggle_mode', true);

        var layout = $.extend({}, loaded);
        var rowCollection = this.layout.editor.currentView.rows.currentView;
        var rows = layout.rows; // Once stashed in a var,

        ...
        // Doesn't make use of `loaded` parameter.  Only modifications are to `layout` variable.

    }

XAML中的我的DataGrid

public sealed class ViewModel : INotifyPropertyChanged
{
    private DataTable data;
    public DataTable Data
    {
        get { return data; }
        set { data = value; OnPropertyChanged("Data"); }
    }

    private char[] head;
    public char[] Head
    {
        get { return head; }
        set { head = value; OnPropertyChanged("Head"); }
    }

    private char name;
    public char Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }
    private static readonly ViewModel instance = new ViewModel();

    public static ViewModel Instance
    {
        get { return instance; }
    } 
    private ViewModel()
    {
        data = new DataTable();
    }

    protected void OnPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我有这样的想法:将任何集合和每个项目绑定到一个DataGrid。当然,我不知道该系列中会有多少物品。

<DataGrid Name="dtgOutput" Grid.Row="0" Grid.Column="2" AutoGenerateColumns="True" IsReadOnly="True" Width="Auto" Height="Auto" VerticalAlignment="Top"
                  ItemsSource="{Binding Data, diag:PresentationTraceSources.TraceLevel=High}" HeadersVisibility="All">
  <DataGrid.RowHeaderTemplate>
    <DataTemplate>
      <TextBlock Text="Row header" />
    </DataTemplate>
  </DataGrid.RowHeaderTemplate>
  <DataGrid.Resources>
    <Style TargetType="Button" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
      <Setter Property="Content" Value="{Binding Name}" />
    </Style>
  </DataGrid.Resources>
</DataGrid> 

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

不要将数据表绑定到数据网格,而是尝试将其绑定到数据视图。

$scope.displayNum = 10;
$scope.increaseBy = function(num) {
  $scope.displayNum +=num;
}

View Model将包含表格列表,例如:

DataTable table = new DataTable();
DataView view = table.DefaultView;

以上也可以转移到xaml。

现在希望这解决了属性更改通知的问题。