MainWindow.xaml.cs :
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using Prism.Commands;
using Timer = System.Timers.Timer;
namespace FlexGridDemo;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() { InitializeComponent();
FlexGridL1.ItemsSource =new ObservableCollection<ElementItem> {
new("A0", 1),
new("A1", 1),
new("A2", 1),
new("A3", 1),
new("A4", 1),
new("A5", 1)
};
FlexGridL1.FrozenRows = 7;
}
private void Add_OnClick(object sender, RoutedEventArgs e) {
FlexGridL1.FrozenRows = 5;
FlexGridL1.ItemsSource = new ObservableCollection<ElementItem> {
new("A0", 1),
new("A1", 1),
new("A3", 1),
new("A4", 1),
new("A5", 1)
};
}
public class ElementItem {
public ElementItem(string elementName, double elementValue) {
ElementName = elementName;
ElementValue = elementValue;
}
public string ElementName { get; set; }
public double ElementValue { get; set; }
}
}
MainWindow.xaml :
<Window
Height="450"
Title="MainWindow"
Width="800"
mc:Ignorable="d"
x:Class="FlexGridDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="Grid">
<Grid.Resources>
<Style TargetType="c1:FlexGrid" x:Key="Style">
<Setter Property="DefaultRowHeight" Value="Auto" />
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="c1:GridCellView">
<Setter Property="Foreground" Value="#1C1C1C" />
<Setter Property="ToolTipService.InitialShowDelay" Value="1000" />
<Setter Property="ToolTipService.ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content.Child .Text}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightGray" />
</Trigger>
<Trigger Property="ToolTipService.ToolTip" Value="">
<Setter Property="ToolTip.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<c1:FlexGrid
AutoGenerateColumns="False"
BorderThickness="1,0,1,0"
ColumnHeaderGridLinesVisibility="All"
FrozenLinesBrush="Black"
FrozenRows="{Binding FrozenRows}"
Grid.Column="1"
GridLinesVisibility="Vertical"
HeadersVisibility="Column"
Height="Auto"
IsReadOnly="True"
MinWidth="200"
RowHeaderGridLinesVisibility="All"
SelectionMode="RowRange"
Style="{StaticResource Style}"
TopLeftHeaderGridLinesVisibility="All"
VerticalScrollBarVisibility="Auto"
Width="Auto"
x:Name="FlexGridL1">
<c1:FlexGrid.Columns>
<c1:GridColumn Header="元素" HeaderHorizontalAlignment="Center">
<c1:GridColumn.CellTemplate>
<DataTemplate>
<Border BorderBrush="#D0D0D0" BorderThickness="0,0,0,1">
<TextBlock
Margin="5,0,0,0"
Text="{Binding ElementName}"
TextAlignment="Left" />
</Border>
</DataTemplate>
</c1:GridColumn.CellTemplate>
</c1:GridColumn>
<c1:GridColumn
Binding="ElementValue"
Header="值"
HeaderHorizontalAlignment="Center">
<c1:GridColumn.CellTemplate>
<DataTemplate>
<Border BorderBrush="#D0D0D0" BorderThickness="0,0,0,1">
<TextBlock
Margin="5,0,0,0"
Text="{Binding ElementValue}"
TextAlignment="Right" />
</Border>
</DataTemplate>
</c1:GridColumn.CellTemplate>
</c1:GridColumn>
</c1:FlexGrid.Columns>
</c1:FlexGrid>
<Button Click="Add_OnClick" Grid.Column="2">Add</Button>
</Grid>
</Window>
|