Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

XML
<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>


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:

C#
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);
        }
    }
 
Share this answer
 
Comments
barrelish 28-Nov-10 9:37am    
Hi ,69Icaro .
Thank you for your answer.
But I can't use your code in my App,because there is a error.
my namespace can't use the datagrid.
I don't know the reason. Could you tell me!
barrelish 28-Nov-10 9:38am    
Thank you for your reply,I deleted the answer.I will find the toolfit to try it .
69Icaro 28-Nov-10 9:59am    
Ok. Let me know!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900