Skip to content

Latest commit

 

History

History
398 lines (302 loc) · 12.1 KB

README.md

File metadata and controls

398 lines (302 loc) · 12.1 KB

FlexGrid

Build And Publish to Nuget Nuget
.NET  .NET  .NET 
CodeFactor  License  C#  WPF 

FlexGrid is a custom WPF DataGrid with convenient and useful features. When developing code using WPF, the Microsoft-supported DataGrid has limited functionality, such as nested and merged column headers and variable columns. However, with FlexGrid, your DataGrid development environment becomes significantly more convenient!


I'm proud to say that FlexGrid was fully built by me, and I'm excited to share it on GitHub :)


Goal

  • Create Customized DataGrid with convenient and useful features.
  • Use FlexGrid to develop other WPF Programs

Available Features


FlexGrid Template Structure

FlexGrid is a Component that modified the Template of the default DataGrid.

FlexGridTemplateStructure.png
<FlexGrid Template Structure>

Using Bands Instead Of Columns

FlexGrid uses BandHeadersPresenters to represent the columns. The BandHeaderPresenter represents the Bands in FlexGrid.FrozenBands and FlexGrid.Bands as Columns in the FlexGrid.

Band Types

  • TextBand
  • CheckBoxBand
  • ComboBoxBand
  • TemplateBand
  • VirtualBand
    • VirtualTextBand
    • VirtualCheckBoxBand
    • VirtualComboBoxBand
    • VirtualTemplateBand

This is Example how to use Bands.

<!-- xmlns:c="clr-namespace:KevinComponent;assembly=KevinComponent" -->

<c:FlexGrid>
  <!-- Here is start of code to add Bands. -->
  <c:FlexGrid.Bands>

    <!-- TextBand -->
    <c:TextBand
      Width="100"
      HorizontalAlignment="Center"
      Header="Name"
      TextBinding="{Binding Name}" />

    <!-- TemplateBand -->
    <c:TemplateBand Width="250" Header="WebSite">
      <c:TemplateBand.CellTemplate>
        <DataTemplate>
          <TextBlock>
            <Hyperlink
              NavigateUri="{Binding WebSite}"
              RequestNavigate="OnHyperlinkRequestNavigate">
              <TextBlock Text="{Binding WebSite}" />
            </Hyperlink>
          </TextBlock>
        </DataTemplate>
      </c:TemplateBand.CellTemplate>

      <c:TemplateBand.CellEditingTemplate>
        <DataTemplate>
          <TextBox Text="{Binding WebSite}" />
        </DataTemplate>
      </c:TemplateBand.CellEditingTemplate>
    </c:TemplateBand>

  </c:FlexGrid.Bands>
</c:FlexGrid>

Bands and Frozen Bands

For represent Frozen Columns (Always showed Columns) in FlexGrid. You should use FlexGrid.FrozenBands.
The FlexGrid shows to Bands in FlexGrid.FrozenBands as Frozen Columns.

This is Example Code how to use Frozen Bands.

<!-- xmlns:c="clr-namespace:KevinComponent;assembly=KevinComponent" -->

<c:FlexGrid>
  <!-- Here is start of code to add frozen bands. -->
  <c:FlexGrid.FrozenBands>

    <c:TextBand
      Width="100"
      HorizontalAlignment="Center"
      Header="Name"
      TextBinding="{Binding Name}" />

    <c:TextBand
      Width="150"
      HorizontalAlignment="Center"
      Header="BirthDate"
      TextBinding="{Binding BirthDate}" />

  </c:FlexGrid.FrozenBands>

  <!-- code to add default bands. -->
  <c:FlexGrid.Bands>

    <c:TextBand
      Width="200"
      Header="Address"
      TextBinding="{Binding Address}" />

  </c:FlexGrid.Bands>
</c:FlexGrid>

Mergable Column Header (Band.Bands)

The Bands Property in Band can be used to represent Merged Column Headers.

FlexGridMergedHeader.png
<Merged Column Headers>

Related StackOverflow questions:

This is Example Code how to use Band.Bands Object.

<!-- xmlns:c="clr-namespace:KevinComponent;assembly=KevinComponent" -->

<c:FlexGrid>
  <c:FlexGrid.Bands>

    <!-- Here is start of code to add root band. -->
    <c:TextBand Header="Information">
      <!-- code to add sub bands. -->
      <c:TextBand.Bands>

        <!-- TextBand -->
        <c:TextBand
          Width="100"
          HorizontalAlignment="Center"
          Header="Name"
          TextBinding="{Binding Name}" />

        <!-- TextBand -->
        <c:TextBand
          Width="150"
          HorizontalAlignment="Center"
          Header="BirthDate"
          TextBinding="{Binding BirthDate}" />

        <!-- TextBand -->
        <c:TextBand
          Width="200"
          Header="Address"
          TextBinding="{Binding Address}" />

        <!-- TemplateBand -->
        <c:TemplateBand Width="250" Header="WebSite">
          <c:TemplateBand.CellTemplate>
            <DataTemplate>
              <TextBlock>
                <Hyperlink NavigateUri="{Binding WebSite}" RequestNavigate="OnHyperlinkRequestNavigate">
                  <TextBlock Text="{Binding WebSite}" />
                </Hyperlink>
              </TextBlock>
            </DataTemplate>
          </c:TemplateBand.CellTemplate>

          <c:TemplateBand.CellEditingTemplate>
            <DataTemplate>
              <TextBox Text="{Binding WebSite}" />
            </DataTemplate>
          </c:TemplateBand.CellEditingTemplate>
        </c:TemplateBand>

      </c:TextBand.Bands>
    </c:TextBand>

  </c:FlexGrid.Bands>
</c:FlexGrid>

Variable Columns (VirtualBand & VirtualBandBinding)

FlexGrid can represent varaible columns by VirtualBand class.

List of All kind of VirtualBand.

  • VirtualTextBand
  • VirtualCheckBoxBand
  • VirtualComboBoxBand
  • VirtualTemplateBand

Related StackOverflow questions:

Related CodeProject Articles:


This is Example Code how to use VirtualBands.

<!-- xmlns:c="clr-namespace:KevinComponent;assembly=KevinComponent" -->

<c:FlexGrid>
  <c:FlexGrid.FrozenBands>
    <c:TextBand
      Width="100"
      HorizontalAlignment="Center"
      Header="Name"
      TextBinding="{Binding Name}" />

    <c:TextBand
      Width="150"
      HorizontalAlignment="Center"
      Header="BirthDate"
      TextBinding="{Binding BirthDate}" />

    <c:TextBand
      Width="150"
      Header="Address"
      TextBinding="{Binding Address}" />
  </c:FlexGrid.FrozenBands>

  <c:FlexGrid.Bands>
    <c:TextBand Header="Subject Scores">
      <c:TextBand.Bands>
        <c:VirtualTemplateBand
          x:Name="vbandSubjects"
          Width="100"
          HeaderBinding="{Binding Name}">
          <c:VirtualTemplateBand.CellTemplate>
            <DataTemplate>
              <StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
                <TextBlock Text="{c:VirtualBandBinding Grade}" />
                <TextBlock Text="(" />
                <TextBlock Text="{c:VirtualBandBinding Value}" />
                <TextBlock Text=")" />
              </StackPanel>
            </DataTemplate>
          </c:VirtualTemplateBand.CellTemplate>

          <c:VirtualTemplateBand.CellEditingTemplate>
            <DataTemplate>
              <TextBox
                VerticalContentAlignment="Center"
                Text="{c:VirtualBandBinding Value}"
                TextAlignment="Center" />
            </DataTemplate>
          </c:VirtualTemplateBand.CellEditingTemplate>
        </c:VirtualTemplateBand>
      </c:TextBand.Bands>
    </c:TextBand>
  </c:FlexGrid.Bands>
</c:FlexGrid>

The VirtualBandBinding is the class to binding property to generated bands by VirtualBand. FlexGrid converts the Items in the VirtualBand to Columns, while mapping the VirtualBandBinding to each property appropriately so that the data appears in the Cell.

Refer to the code below.

private void UpdateVirtualBandBindings(ContentPresenter cp)
{
if (DerivationFrom == null && DataContext == null)
return;
var propertyAndBindings = Utils.GetBindings<IVirtualBandBindable>(cp);
foreach (var (element, property, bindingBase) in propertyAndBindings)
{
var cell = FlexGridAssist.GetParentCell(cp);
if (cell == null)
continue;
if (Utils.GetIndexerValue(cell.DataContext, new object[] { DataContext }, out object? bindingSource))
{
var newBindingBase = Utils.CloneBinding((BindingBase)bindingBase);
SetBindingSource(newBindingBase, bindingSource);
BindingOperations.SetBinding(element, property, newBindingBase);
}
else
{
var newBindingBase = Utils.CloneBinding((BindingBase)bindingBase);
SetBindingSource(newBindingBase, null);
BindingOperations.SetBinding(element, property, newBindingBase);
}
}
}


Samples

Basic Sample

BasicSample.gif
<BasicSample ScreenShot>

  • BasicSample shows the basic usage of FlexGrid.
  • You can know how to use FlexGrid the basically in this sample.

Frozen Header Sample

BasicSample.gif
<FrozenHedaerSample ScreenShot>

  • FrozenHedaerSample shows how to using the frozen columns.
  • You can use FlexGrid.FrozenBands to add frozen columns.
  • In this sample, the Name, BirthDate bands are Frozen Bands.

Merged Header Sample

BasicSample.gif
<MergedHedaerSample ScreenShot>

  • MergedHedaerSample shows how to merge column headers.
  • You can use Band.Bands(ex. TextBand.Bands, CheckBoxBand.Bands, etc.) to merge column headers.
  • In this sample, you can see the Name, BirthDate, Address, and WebSite bands merged into the information band.

VirtualBand Sample

VirtualBandSample.gif
<VirtualBandSample ScreenShot>

  • VirtualBandSample shows how variable columns are implemented in FlexGrid.
  • You can use VirtualBand(ex. VirtualTextBand, VirtualComboBoxBand, VirtualCheckBoxBand, etc.) to show variable columns.
  • In this Sample, you can see that the list of subject scores synchronizes with the subject list when you edit the subject list.

VirtualBandSample.gif
<ItemsSource Description>


Colored BandHeader Sample

ColoredBandHeaderSample.png
<ColoredBandHeaderSample ScreenShot>

  • ColoredBandHeaderSample shows how to set header and cell style.
  • You can use Band.HeaderStyle and Band.CellStyle to set header style and cell style.

Support

If you have any good ideas (such as new featrue, refactoring, improvement feature quality, etc), do not hesitate to let me know!
You also can "Pull request" or request adding New Feature to the email below. Thank you.