我似乎只能找到ObservableCollections和自动生成列的解决方案,但我没有固定数量的列。这就是我使用DataTable绑定到DataGrid的原因。我不知道如果有可能使嵌套集合绑定有点像这样:
public ObservableCollection<RowData> Rows { get; set;}
public Class RowData
{
public String ColumnName {get; set;}
public ObservableCollection<CellModel> Cells { get; set;}
}
到Datagrid。
我曾尝试使用此answer,但附加属性不会更新所选单元格,我确实相信它,因为我使用的是DataTable。如果它不让我发布一些代码。 它的作用基本上是显示excel表,因此它在一个标签内。
<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,每个tab都有1个。因此问题可能与插入标签的方式有关。
答案 0 :(得分:0)
我没有意识到在向绑定项添加新列表之前它永远不会工作,选择会起作用。
所以基本上是在界限上
Behaviors:DataGridHelper.SelectedCells="{Binding Path=CellSelections, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
在视图模型中我只需要放一个;
CellSelections = new List<DataGridCellInfo>();
在附加财产实际运作之前。
我设法创建一个带有动态对象的Observable Collection,查找ExpandoObject,以便在这里进行大量的帖子og stackoverflow。