簡體   English   中英

如何獲得綁定到DataTable的DataGrid SelectionMode =“ Extended” SelectionUnit =“ Cell”的選擇

[英]How to get selection of DataGrid SelectionMode=“Extended” SelectionUnit=“Cell” Binded to DataTable

我似乎只能用ObservableCollections查找解決方案並自動生成列,但是我沒有固定數量的列。 這就是為什么我使用DataTable綁定到DataGrid的原因。 我不知道是否有可能使嵌套集合綁定如下所示:

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

到Datagrid。

我嘗試使用此答案 ,但是附加屬性不會更新“選定的單元格”,並且我確實相信它是因為我正在使用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");
        }
    }

}

我正在使用的受攻擊財產看起來像這樣,它是從這里來的

#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);
        //}
    }

編輯:

我注意到,附加行為的構造函數只是被觸發的,因此應該有4個gridview,每個選項卡1個。 因此,問題可能與選項卡的插入方式有關。

我沒意識到在將新列表添加到綁定項目之前,它永遠都行不通,選擇將起作用。

所以基本上它的界限

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

在視圖模型中,我只需要放置一個;

CellSelections = new List<DataGridCellInfo>();

在附加屬性實際起作用之前。

我設法創建了一個帶有動態對象的Observable集合,查找ExpandoObject,以便在這里進行很多有關stackoverflow的主題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM