2016-04-05 11 views
0

ObservableCollectionsと自動生成列でソリューションを見つけることはできますが、固定量の列はありません。そのため、私はDataTableをDataGridにバインドするために使用しています。データグリッドにDataGrid SelectionMode = "Extended"の選択方法DataTableにバインドされたSelectionUnit = "Cell"

public ObservableCollection<RowData> Rows { get; set;} 
public Class RowData 
{ 
    public String ColumnName {get; set;} 
    public ObservableCollection<CellModel> Cells { get; set;} 
} 

:私はこのようなやや結合ネストされたコレクションを作成するためにその可能ならば知りません。

私はこのanswerを使用しようとしましたが、アタッチされたプロパティは選択されたセルを更新しません。その理由は、DataTableを使用しているからです。場合は、私はいくつかのコードを投稿させてください。 それは基本的に優れたシートを示しているので、そこにタブがあります。バインドされ何

<TabControl TabStripPlacement="Bottom" 
          Height="400" Width="700" 
          ItemsSource="{Binding SheetTabs}" 
          SelectedValue="{Binding SelectedTab}"> 
        <TabControl.ItemTemplate> 
         <DataTemplate> 
          <TextBlock 
           Text="{Binding SheetName}" /> 
         </DataTemplate> 
        </TabControl.ItemTemplate> 
        <TabControl.ContentTemplate> 
         <DataTemplate> 
          <DataGrid 
          ItemsSource="{Binding SheetData}" 
           SelectionUnit="Cell" 
           Behaviors:DataGridHelper.SelectedCells="{Binding Path=CellSelections, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
           SelectionMode="Extended" 
           LoadingRow="DataGrid_LoadingRow" 
           RowHeaderWidth="30"> 

          </DataGrid> 
         </DataTemplate> 
        </TabControl.ContentTemplate> 
       </TabControl> 

は、このクラスののObservableCollectionです:私はこのようなルックスを使用しています

public class SheetViewModel : BaseViewModel 
{ 
    public string SheetName { get; set; } 
    public DataTable SheetData { get; set; } 
    private IList<DataGridCellInfo> _cellSelection; 
    public IList<DataGridCellInfo> CellSelections { 
     get 
     { 
      return _cellSelection; 
     } 
     set 
     { 
      _cellSelection = value; 
      NotifyPropertyChange("CellSelections"); 
     } 
    } 

} 

攻撃の特性、そのhereから盗まれました。

#region SelectedCells 
    public static IList<DataGridCellInfo> GetSelectedCells(DependencyObject obj) 
    { 
     return (IList<DataGridCellInfo>)obj.GetValue(SelectedCellsProperty); 
    } 
    public static void SetSelectedCells(DependencyObject obj, IList<DataGridCellInfo> value) 
    { 
     obj.SetValue(SelectedCellsProperty, value); 
    } 
    public static readonly DependencyProperty SelectedCellsProperty = 
     DependencyProperty.RegisterAttached("SelectedCells", typeof(IList<DataGridCellInfo>), typeof(DataGridHelper), new UIPropertyMetadata(null, OnSelectedCellsChanged)); 
    static SelectedCellsChangedEventHandler GetSelectionChangedHandler(DependencyObject obj) 
    { 
     return (SelectedCellsChangedEventHandler)obj.GetValue(SelectionChangedHandlerProperty); 
    } 
    static void SetSelectionChangedHandler(DependencyObject obj, SelectedCellsChangedEventHandler value) 
    { 
     obj.SetValue(SelectionChangedHandlerProperty, value); 
    } 
    static readonly DependencyProperty SelectionChangedHandlerProperty = 
     DependencyProperty.RegisterAttached("SelectedCellsChangedEventHandler", typeof(SelectedCellsChangedEventHandler), typeof(DataGridHelper), new UIPropertyMetadata(null)); 

    //d is MultiSelector (d as ListBox not supported) 
    static void OnSelectedCellsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args) 
    { 
     if (GetSelectionChangedHandler(d) != null) 
      return; 

     if (d is DataGrid)//DataGrid 
     { 
      DataGrid datagrid = d as DataGrid; 
      SelectedCellsChangedEventHandler selectionchanged = null; 
      foreach (var selected in GetSelectedCells(d) as IList<DataGridCellInfo>) 
       datagrid.SelectedCells.Add(selected); 

      selectionchanged = (sender, e) => 
      { 
       SetSelectedCells(d, datagrid.SelectedCells); 
      }; 
      SetSelectionChangedHandler(d, selectionchanged); 
      datagrid.SelectedCellsChanged += GetSelectionChangedHandler(d); 
     } 
     //else if (d is ListBox) 
     //{ 
     // ListBox listbox = d as ListBox; 
     // SelectionChangedEventHandler selectionchanged = null; 

     // selectionchanged = (sender, e) => 
     // { 
     //  SetSelectedCells(d, listbox.SelectedCells); 
     // }; 
     // SetSelectionChangedHandler(d, selectionchanged); 
     // listbox.SelectionChanged += GetSelectionChangedHandler(d); 
     //} 
    } 

編集:

私は付属の行動のための建設業者がものをだけ発射されることに気づいた、eventho 4 gridviews、各タブの1があるはずです。多分問題は、タブの挿入方法と関係があります。

答えて

0

バインドされたアイテムに新しいリストを追加する前に決してうまくいかないことに気が付きませんでした。選択が機能します。

だから、基本的に私はちょうど置くために必要なビューモデルでバウンド

Behaviors:DataGridHelper.SelectedCells="{Binding Path=CellSelections, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 

に。

CellSelections = new List<DataGridCellInfo>(); 

添付プロパティが実際に動作する前。

私は動的オブジェクトでObservable Collectionを作成して、ExpandoObjectを検索しました。

関連する問題