Coder Social home page Coder Social logo

gu.wpf.datagrid2d's Introduction

Gu.Wpf.DataGrid2D

Gitter Chat Room License NuGet ci

Attached properties for WPF DataGrid enabling binding to sources of different types. For using DataGrid2D you need to add xmlns:dataGrid2D="http://gu.se/DataGrid2D" in XAML

Contents

ItemsSource.Array2D & Array2DTransposed

For binding to sources of type T[,]

Array2D

<DataGrid HeadersVisibility="None"
          dataGrid2D:ItemsSource.Array2D="{Binding Data2D}" />

Renders:
ItemsSource2D render

Explicit columns

Columns are referred to by C<zero_based_index>

<DataGrid AutoGenerateColumns="False"
          dataGrid2D:ItemsSource.Array2DTransposed="{Binding Data2D}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding C0}" Header="Col 1" />
        <DataGridTextColumn Binding="{Binding C1}" Header="Col 2" />
        <DataGridTextColumn Binding="{Binding C2}" Header="Col 3" />
    </DataGrid.Columns>
</DataGrid>

Renders:
ItemsSource2D render

With headers:

<DataGrid dataGrid2D:ItemsSource.Array2D="{Binding Data2D}"
          dataGrid2D:ItemsSource.ColumnHeadersSource="{Binding ColumnHeaders}"
          dataGrid2D:ItemsSource.RowHeadersSource="{Binding RowHeaders}" />

Renders:
With headers screenie

Array2DTransposed

<DataGrid dataGrid2D:ItemsSource.Array2DTransposed="{Binding Data2D}" />

Renders:
ItemsSource2D render

ItemsSource.RowsSource & ColumnsSource

Lets you bind to datasources of type IEnumerable<IEnumerable>>. Tracks collection changes.

RowsSource

<DataGrid HeadersVisibility="None"
          dataGrid2D:ItemsSource.RowsSource="{Binding ListOfListsOfInts}" />

Renders:
ItemsSource2D render

ColumnsSource

<DataGrid HeadersVisibility="None"
          dataGrid2D:ItemsSource.ColumnsSource="{Binding ListOfListsOfInts}" />

Renders:
ItemsSource2D render

Different lengths

Limited support for different lengths. Columns with blanks are default readonly.

<DataGrid dataGrid2D:ItemsSource.RowsSource="{Binding DifferentLengths}" />

Renders:
ItemsSource2D render

Selected.CellItem & Index

Lets you two-way bind the item of the currently selected cell or index (row, col). For this to work these conditions must be satisfied:

  • SelectionUnit="Cell"
  • Columns must be of type DataGridBoundColumn. Don't think there is a way to dig out the bound property of a DataGridTemplateColumn
<DataGrid SelectionUnit="Cell"
          dataGrid2D:ItemsSource.RowsSource="{Binding RowVms}"
          dataGrid2D:Selected.CellItem="{Binding SelectedItem}"
          dataGrid2D:Selected.Index="{Binding Index}" />

ItemsSource.TransposedSource & PropertySource

Support for transposing an itemssource, perhaps useful for property grid scenarios. Supports binding to single item or (Observable)Collection

PropertySource

Same as TransposedSource but for a single item.

Renders:
ItemsSource2D render

TransposedSource with explicit columns

The property name column is named Name and the following columns are named C<zero_based_index>

<DataGrid AutoGenerateColumns="False" 
          dataGrid2D:ItemsSource.TransposedSource="{Binding Persons}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Name}" Header="Property" />
        <DataGridTextColumn Binding="{Binding C0}" Header="Value 1" />
        <DataGridTextColumn Binding="{Binding C1}" Header="Value 2" />
    </DataGrid.Columns>
</DataGrid>

Renders:
ItemsSource2D render

Rownumbers

Convenience attached property if you want to display rownumbers. Specify the number to start fom using StartAt

<DataGrid ItemsSource="{Binding Persons}" dataGrid2D:Index.StartAt="1">
    <DataGrid.RowHeaderStyle>
        <Style TargetType="{x:Type DataGridRowHeader}">
            <Setter Property="Content" Value="{Binding Path=(dataGrid2D:Index.OfRow), RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" />
        </Style>
    </DataGrid.RowHeaderStyle>
</DataGrid>

Renders:
Rownumbers render

gu.wpf.datagrid2d's People

Contributors

dependabot[bot] avatar foggyfinder avatar gitter-badger avatar johanlarsson avatar maverik avatar mennowo avatar venomed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

gu.wpf.datagrid2d's Issues

How to make TwoWay binding with templates?

Hello! Can anybody help me? I wanna bind 2D int array to DataGrid, but I don't understand how to create TwoWay binding. Now I'm use this code:

<DataGrid Grid.Column="1" Grid.Row="1" x:Name="AutoColumns"
    dataGrid2D:ItemsSource.Array2D="{Binding Path=Data2D, Mode=TwoWay}"
    dataGrid2D:ItemsSource.ColumnHeadersSource="{Binding ColumnHeaders}"
    dataGrid2D:ItemsSource.RowHeadersSource="{Binding RowHeaders}"
    ColumnWidth="50"
    SelectionUnit="Cell"
    IsReadOnly="False"
    IsEnabled="True">
<dataGrid2D:Cell.Template>
<DataTemplate>
    <TextBlock Text="{Binding Path=., StringFormat=X4}" IsEnabled="true"/>
</DataTemplate>
</dataGrid2D:Cell.Template>
    <dataGrid2D:Cell.EditingTemplate>
    <DataTemplate>
        <TextBox Text="{Binding Path=., StringFormat=X4}" />
    </DataTemplate>
</dataGrid2D:Cell.EditingTemplate>
</DataGrid>

Also I'm tried to add an attributes like "Mode=TwoWay, BindsDirectlyToSource=True, UpdateSourceTrigger=LostFocus, NotifyOnSourceUpdated=True" In data templates, but it's do nothing effect. Now it's allow me to entering some numbers in cells, but not save to my array. And one detail - everything work good without templates, but I need them.

ObservableArray2D<T>

For wrapping T[] in a way that is twoway bindable.
Not sure what a nice API would be, creating an issue for bikeshedding.

Bug when using Cell.EditingTemplate

<DataGrid dataGrid2D:ItemsSource.Array2D="{Binding Array2D}">
    <dataGrid2D:Cell.Template>
        <DataTemplate>
            <TextBlock Text="{Binding StringFormat=E1}" />
        </DataTemplate>
    </dataGrid2D:Cell.Template>
    <dataGrid2D:Cell.EditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding}" />
        </DataTemplate>
    </dataGrid2D:Cell.EditingTemplate>
</DataGrid>
this.Array2D = new[,] { { 1.0, 2 }, { 3, 4 }, { 5, 6 } };

Cells/Row/Column sizes.

Hi,

I'm using your library for one of my projects and I love it.
My issue is that I need a way to determine the actual size of each cell , or alternatively the size of each row and column.
Do you think there is a way of doing this?

Thank you!

Virtualization Problem

When scrolling, the cells only display the first Item in the RowsSource
I use a BindableCollection<BindableCollection> as RowsSource.
If I set EnableRowVirtualization="False" the problem disappears, but of course the memory consumption is tremendous.

Get list of all selected cells

It is currenly only possible via Selected.CellItem to get an single cell item. I want to bind multiple selected cells to my ViewModel, which would require something like Selected.CellItems.

Sort not working with custom cell template

When using the option to have custom cell template, eg.:

dataGrid2D:Cell.Template="{StaticResource CellTemplate}"

sorting via clicking on columns is no longer possible. Even if, for example, I simply bind double values. It would be nice to have this option, would it be doable?

Single click edit.

If possible make an attached property that allows for single click edit of cells.

Scientific Notation for Numeric Data

Thank you for this amazing user control. I'm currently trying to use it to display large numbers best represented in scientific notation in my project.

I have tried modifying your code by manually assigning StringFormat of Binding and ItemStringFormatProperty of ItemControl. But the format of the result stays the same.

Can anyone please point me to the right direction on how this can be implemented?

Thanks.

Binding RowheaderSourceProperty invalid cast exception

Currently, for a project I'm using three lists to populate a datagrid through bindings. They are column headers, row header and row source (Latter being a list of lists).
The debugger caught an exception when trying to assign null to data context (DataContext=null;) in .dll of this package (Gu.Wpf....).

Trying to narrow the origin of the problem I could trace it to the following line: "BindingOperations.ClearBinding(Table, Gu.Wpf.DataGrid2D.ItemsSource.RowHeadersSourceProperty);"

The error is the following: InvalidCastException was unhandled. unable to cast Gu.Wpf.DataGrid2D.ListRowView to type System.Windows.Controls.DataGridRow.

The problem doesnot arise when I first clear rowsource binding.

Am I mising something on the use of this package?

Insert to ObservableCollection<ObservableCollection<T>> set as RowSource throws

Hey,

started playing around with your extensions as I am building simple table designer control in WPF and your library is a good kickstarter as we can use really tabular items source (collection of collections). Really cool, thank you for that!

I was working on a POC where I was implementing drag/drop reorder of columns and rows and realized that your extension has a bug when inserting a row to different index than the last index (simple add). If you insert a row to the ObservableCollection bound as RowSource to index 0, ListCollectionView will throw InvalidOperationException.

You can reproduce it easily in your Demo project by changing this line to this.Data.Insert(0, new ObservableCollection<int>(newRow)); instead of Add and then on second call it will fail.

The exception is thrown because the List2DView doesn't actually insert the row internally, but adds it to the end. I had a quick look and the root cause is this line where you should call this.Rows.Insert(index, listRowView); instead of this.Rows.Add(listRowView);. I think the fix will be bit more difficult, because indexes of ListRowViews following the insertion need to be updated, which is not possible at the moment as they are only readonly properties and also the property list index descriptors I believe, so it seems also like architectural change.

Do you see a simple way of fixing it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.