如何自定义单元格以使用WPF显示数据? [英] How to custome the cell to display data using WPF?

查看:136
本文介绍了如何自定义单元格以使用WPF显示数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个想法可以用这种方式显示数据.
我查询了数据库中的一条记录,该记录具有三个字段(例如a,b,c).
然后,我想在同一行的两列中显示这些字段.字段a在左列中.右列分为两行. b在第一行中,c在臀部行中.我想在右列的第一行中添加一个控件(例如按钮),以通过单击它来更改数据.
我想使用C#和WPF.

谁可以帮助我?
谢谢你.

I have an idea to display data in this way.
I qurey a record from the database,this record has three fields(for example a,b,c).
Then I want display these fields in two columns of the same row. The field a is in the left column. The right column is divided into two rows. b is in the top row,c is in the buttom row. And I want add a control(such as button) in the top row of the right column to change the data with clicking it.
I want to use C# and WPF.

Who can help me?
Thank u.

推荐答案

<Window x:Class="WpfApplication1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <DataGrid Name="dg" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="A">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ContentPresenter Content="{Binding a}" HorizontalAlignment="Left" VerticalAlignment="Center" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="B + C">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <ContentPresenter Content="{Binding b}" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
                            <Rectangle Grid.Row="1" Grid.ColumnSpan="2" Margin="0" Stroke="Black" StrokeThickness="1" />
                            <ContentPresenter Content="{Binding c}" Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" />
                            <Button Grid.Row="0" Grid.Column="1" Content="Edit" Margin="15,5,5,5" HorizontalAlignment="Right" Tag="{Binding}" Click="Button_Click" />
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Window>



这是要使用的XAML代码:DataGrid仅具有2列,第二个具有不同的模板.我已经使用网格在第二列中制作了2个不同的行.按钮的Tag属性绑定到要编辑的对象,因此,在后面的代码中,我们可以执行以下操作:



This is the XAML code to use: the DataGrid has only 2 columns and the second one has a different template. I''ve used a grid to make 2 different rows in the second column. The Tag property of the button is bound to the object to edit, so, in the code behind, we can do this:

public struct Structure
    {
        public string a { get; set; }
        public string b { get; set; }
        public string c { get; set; }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            dg.ItemsSource = new Structure[] { new Structure { a = "", b = "", c = "" }, new Structure { a = "1", b = "2", c = "3" } };
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //Gets the object to edit
            Structure obj = (Structure)((Button)sender).Tag;
            //Msgbox
            MessageBox.Show("Object to edit:\r\nA: " + obj.a + "\r\nB: " + obj.b + "\r\nC: " + obj.c);
        }
    }


这篇关于如何自定义单元格以使用WPF显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆