如何使用其列的混合内容创建datagrid?

问题描述 投票:0回答:1

我有类Calc进行一些计算,输入参数是:

  • int a
  • int b
  • bool c

输出参数:

  • int d = a + b
  • double e = (a * a) / (b * b)
  • bool f = !c

我创建了ObservableCollection,它包含两个计算的输入和输出数据。我需要创建包含以下内容的表:

===================================
| Parameter | Calc 1   | Calc   2 |
===================================
| a         | [     1] | [     5] |
| b         | [     2] | [    10] |
| c         | [x]      | [ ]      |
| d         | 3        | 15       |
| e         | 0.25     | 0.25     |
| f         | false    | true     |
===================================

第一列包含参数的名称,第二列,第三列 - 每个calc的输入和输出参数。前两行应包含输入,以便用户可以键入输入数据的新值以进行计算。第三行应包含复选框。

是否有可能在WPF中实现这样的动态表?

提前致谢。

c# .net wpf wpfdatagrid
1个回答
0
投票

我尽力构建你所要求的东西。当旋转DataGrid时,它具有副作用,即datagrid项的顺序相反。这就是为什么在XAML中反转了datagrid列,你会通过tab-order注意到它。但请尝试以下代码和xaml。

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;

namespace HorizontalDataGridMixedContentFromCalc
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<Calculation> calculations = new ObservableCollection<Calculation>();
        public ObservableCollection<Calculation> Calculations
        {
            get { return calculations; }
            set { calculations = value; }
        }

        public MainWindow()
        {
            InitializeComponent();
            Calculations.Add(new Calculation { a = 3, b = 6, c = true });
            Calculations.Add(new Calculation { a = 3, b = 9, c = false });
            DataContext = this;
        }

        public class Calculation : INotifyPropertyChanged
        {
            private int _a;
            public int a
            {
                get
                {
                    return _a;
                }
                set
                {
                    _a = value;
                    OnPropertyChanged("d");
                    OnPropertyChanged("e");
                }
            }

            private int _b;
            public int b
            {
                get { return _b; }
                set
                {
                    _b = value;
                    OnPropertyChanged("d");
                    OnPropertyChanged("e");
                }
            }

            private bool _c;
            public bool c
            {
                get { return _c; }
                set
                {
                    _c = value;
                    OnPropertyChanged("f");
                }
            }

            public int d
            {
                get
                {
                    return a + b;
                }
            }

            public double e
            {
                get
                {
                    if (b == 0) return 0;
                    return (double)(a * a) / (b * b);
                }
            }

            public bool f
            {
                get
                {
                    return !c;
                }

            }

            protected void OnPropertyChanged(string name)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
            }

            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
}

和XAML:

<Window x:Class="HorizontalDataGridMixedContentFromCalc.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HorizontalDataGridMixedContentFromCalc"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid Name="DataGridTest" 
                  AutoGenerateColumns="False"
                  ItemsSource="{Binding Calculations, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top">
            <DataGrid.Columns>
                <DataGridTextColumn Header="f" Binding="{Binding f}"></DataGridTextColumn>
                <DataGridTextColumn Header="e" Binding="{Binding e}" ></DataGridTextColumn>
                <DataGridTextColumn Header="d" Binding="{Binding d}" ></DataGridTextColumn>
                <DataGridCheckBoxColumn Header="c" Binding="{Binding c}" ></DataGridCheckBoxColumn>
                <DataGridTextColumn Header="b" Binding="{Binding b}" ></DataGridTextColumn>
                <DataGridTextColumn Header="a" Binding="{Binding a}" ></DataGridTextColumn>
            </DataGrid.Columns>
            <DataGrid.LayoutTransform>
                <TransformGroup>
                    <RotateTransform Angle="270"/>
                </TransformGroup>
            </DataGrid.LayoutTransform>
            <DataGrid.ColumnHeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <RotateTransform Angle="90"/>
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.ColumnHeaderStyle>
            <DataGrid.CellStyle>
                <Style TargetType="DataGridCell">
                    <Setter Property="LayoutTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <RotateTransform Angle="90"/>
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.CellStyle>
        </DataGrid>
    </Grid>
</Window>

结果如下所示,当您更改a,b,c输入时,输出会更新:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.