How can I make a similar looking grid?


3D Ancient

I'm going to be writing some desktop applications that simulate something, and I thought it would be a good opportunity to try some new technology. Since the app is for Windows and I saw a Visual Studio Community version somewhere, I decided to try WPF.

So here's the thing. The base view should look like a simple grid where each rectangle is an actual TextBox. I can click and write some text in each text.

base grid

That's not bad. At first, I was playing with Grid and ColumnDefinitions and RowDefinitions, which worked well for hardcoded code. After that, I tried using ItemTemplate's ItemsControl and it almost worked.

But now there is a conspiracy twist. I want to be able to edit each individual TextBox. By edit, I mean split it into several smaller TextBoxes. So if I split the second in two and the third in three, it should look like:

Edit grid

And I don't know how to fix this. Since it's not like the others, I don't think I can use the ItemsControl with templates anymore (or can I?). I'm pretty new to WPF, so maybe there's something obvious I haven't seen. So if someone out there knows WPF very well and can point me in the right direction, or at least tell me "what are you doing? WPF is not for this type of application, use XXX instead".

Kory Gill

That's a good question, there are of course many ways to lay out controls depending on what the app does and how each control's data is wired to the app/model/whatever.

This answer focuses on layout while taking all available space and allowing duplication of content within each container. The content flows smoothly until the window becomes too small, too narrow, etc. This is where you need to decide what the application will allow the user to do. There's still a lot of work to do before this code can be converted to production quality, but this is a good example of getting familiar with some WPF basics of the WPF platform.

I added some borders, margins and background colors for testing so you can see which container takes up what space. Suitable for testing; may be removed or changed to "transparent" in final release.

Wory Unified Grid Sample by Kory Gill

Main window

XAML

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel>
        <TextBlock Text="WPF" FontSize="36" Margin="20" Foreground="Orange" HorizontalAlignment="Center"/>
    </StackPanel>
    <Grid Grid.Row="1" Margin="5">
        <Border Background="LightGray" BorderBrush="Red" BorderThickness="1">
            <UniformGrid Columns="4" Name="MainPanel"/>
        </Border>
    </Grid>
</Grid>

code

public partial class MainWindow : Window
{
    private static int _nextId = 0;
    public static int NextId
    {
        get { return _nextId++; }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        // add non-multiple of 8 to see how layout works
        for (var i=0; i<7; i++)
        {
            MainPanel.Children.Add(new EditPanelControl());
        }
    }
}

EditPanelControl (User Control)

XAML

<Grid Margin="5">
    <Border Background="LightYellow" BorderBrush="Green" BorderThickness="1">
        <!-- Make this a viewbox if you want to show all items but have them shrink -->
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
            <StackPanel Name="MainPanel" VerticalAlignment="Center"/>
        </ScrollViewer>
    </Border>
</Grid>

code

public partial class EditPanelControl : UserControl
{
    public EditPanelControl()
    {
        InitializeComponent();
        DataContext = this;
        Loaded += EditPanelControl_Loaded;
    }

    private void EditPanelControl_Loaded(object sender, RoutedEventArgs e)
    {
        AddSuperTextControl();
    }

    private void AddSuperTextControl()
    {
        var stc = new SuperTextControl();
        stc.SplitEvent += Stc_SplitEvent;
        stc.DeleteEvent += Stc_DeleteEvent;
        stc.SuperTextBox.Text = MainWindow.NextId.ToString();
        MainPanel.Children.Add(stc);
    }

    private void Stc_DeleteEvent(object sender, EventArgs e)
    {
        // todo: don't allow delete if only 1 child
        var stc = (SuperTextControl)sender;
        MainPanel.Children.Remove(stc);
    }

    private void Stc_SplitEvent(object sender, EventArgs e)
    {
        var stc = (SuperTextControl)sender; // fyi
        AddSuperTextControl();
    }
}

SuperTextControl (User Control)

XAML

<Grid Margin="5">
    <Border Background="Wheat" BorderBrush="Blue" BorderThickness="1">
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Stretch">
            <TextBox Name="SuperTextBox" Margin="5"/>
            <DockPanel LastChildFill="False" Margin="0,0,0,5">
                <Button Content="Split" Click="SplitHandler" Margin="5,0" DockPanel.Dock="Left"/>
                <Button Content="Delete" Click="DeleteHandler" Margin="5,0" DockPanel.Dock="Right"/>
            </DockPanel>
        </StackPanel>
    </Border>
</Grid>

code

public partial class SuperTextControl : UserControl
{
    public event EventHandler SplitEvent;
    public event EventHandler DeleteEvent;

    public SuperTextControl()
    {
        InitializeComponent();
    }

    private void SplitHandler(object sender, RoutedEventArgs e)
    {
        var button = (Button)sender; // fyi

        if (SplitEvent != null)
        {
            SplitEvent(this, new EventArgs());
        }
    }

    private void DeleteHandler(object sender, RoutedEventArgs e)
    {
        var button = (Button)sender; // fyi

        if (DeleteEvent != null)
        {
            DeleteEvent(this, new EventArgs());
        }
    }
}

Related


How can I make a similar looking grid?

3D Ancient I'm going to be writing some desktop applications that simulate something, and I thought it would be a good opportunity to try some new technology. Since the app is for Windows and I saw a Visual Studio Community version somewhere, I decided to try

How can I do something similar to this with CSS Grid?

Tlaloc-ES I'm trying to get a grid layout of 6 elements centered on the page like this: But I can't get the grid to be in the center of the image, any ideas? .grid-container { display: grid; grid-template-columns: auto auto auto; back

How can I do something similar to this with CSS Grid?

Tlaloc-ES I'm trying to get a grid layout of 6 elements centered on the page like this: But I can't get the grid to be in the center of the image, any ideas? .grid-container { display: grid; grid-template-columns: auto auto auto; back

How can I make the grid responsive?

Diego Diaz I'm working on CSS grid design and it's working fine on desktop view. ¿ How can I make it responsive? I'll show a section with a background image and add a few articles later to show a grid over the image. Like I said, it worked fine on desktop, but

How can I make the grid responsive?

Diego Diaz I'm working on CSS grid design and it's working fine on desktop view. ¿ How can I make it responsive? I'll show a section with a background image and add a few articles later to show a grid over the image. Like I said, it worked fine on desktop, but

How can I make the grid cover the numbers?

plum I am new to RRDtool . I have generated a graph with grid (-grid-dash 1:0), LINE (LINE1:rt#4e9a06) and also colored areas (AREA:rt#4e9a06) between the line and the x-axis. I noticed that the grid still shows up in the colored area. I am wondering if there

How can I make the grid responsive?

Diego Diaz I'm working on CSS grid design and it's working fine on desktop view. ¿ How can I make it responsive? I'll show a section with a background image and add a few articles later to show a grid over the image. Like I said, it worked fine on desktop, but

How can I make a heatmap in pygame on Grid

JJH562 I'm making a grid on pygame and I'd like to know how I can do this so that starting from a point on that grid, the color changes as you get further from that point, e.g. from dark blue to into a square, light blue when it becomes a ten square away. I'm

How can I make a heatmap in pygame on Grid

JJH562 I'm making a grid on pygame and I'd like to know how I can do this so that starting from a point on that grid, the color changes as you get further from that point, e.g. from dark blue to into a square, light blue when it becomes a ten square away. I'm

How can I make the grid responsive?

Diego Diaz I'm working on CSS grid design and it's working fine on desktop view. ¿ How can I make it responsive? I'll show a section with a background image and add a few articles later to show a grid over the image. Like I said, it worked fine on desktop, but

How can I make the grid responsive?

Diego Diaz I'm working on CSS grid design and it's working fine on desktop view. ¿ How can I make it responsive? I'll show a section with a background image and add a few articles later to show a grid over the image. Like I said, it worked fine on desktop, but

How can I make the grid cover the numbers?

plum I am new to RRDtool . I have generated a graph with grid (-grid-dash 1:0), LINE (LINE1:rt#4e9a06) and also colored areas (AREA:rt#4e9a06) between the line and the x-axis. I noticed that the grid still shows up in the colored area. I am wondering if there

How can I make my database approach more forward looking?

James V I am currently developing a new application in which database data transfer is taking place between two databases. I currently write a method, but it looks like I can make this code more compact. I read an article that uses that statement Usingbut not

How can I make a query similar to SQL query in Firebase?

Pedro Silveira: I am trying to develop an app for Android using Java. I am using a NoSQL Firebase database. However, it is very different from what I have learned so far, so I hope you can help me do the following: SELECT "name" FROM users WHERE email = "user@

How can I make a similar app using Street View?

Elkann Dirikkan Do you have any information on how to proceed with the application, such as what is shown on the link? http://virali.se/table_booking_demo/ User 3454848 You write the code :) First look at the Google Maps API, look closely at the overlay (eg: h

How can I make a query similar to SQL query in Firebase?

Pedro Silveira: I am trying to develop an app for Android using Java. I am using a NoSQL Firebase database. However, it is very different from what I have learned so far, so I hope you can help me do the following: SELECT "name" FROM users WHERE email = "user@

How can I make a query similar to SQL query in Firebase?

Pedro Silveira: I am trying to develop an app for Android using Java. I am using a NoSQL Firebase database. However, it is very different from what I have learned so far, so I hope you can help me do the following: SELECT "name" FROM users WHERE email = "user@

How can I make a query similar to SQL query in Firebase?

Pedro Silveira: I am trying to develop an app for Android using Java. I am using a NoSQL Firebase database. However, it is very different from what I have learned so far, so I hope you can help me do the following: SELECT "name" FROM users WHERE email = "user@

How can I make a query similar to SQL query in Firebase?

Pedro Silveira: I am trying to develop an app for Android using Java. I am using a NoSQL Firebase database. However, it is very different from what I have learned so far, so I hope you can help me do the following: SELECT "name" FROM users WHERE email = "user@

Can I use hlookup or similar to keep looking for specific text?

Cary So this is what I want to do. For example, here is the mockup I made to get functionality. On the left, I have people in columns (B/C) and food in rows (2/3). Where people and food meet in the table, I want to search the data on the right to see if their

Can I use hlookup or similar to keep looking for specific text?

Cary So this is what I want to do. For example, here is the mockup I made to get functionality. On the left, I have people in columns (B/C) and food in rows (2/3). Where people and food meet in the table, I want to search the data on the right to see if their

Can I use the type cast operator on 2 similar looking classes?

Shrinidhisondur Suppose I have the following 2 classes. How can I use type casting to achieve the effect described below? What I want is: I will return this +1 and this -1 because my 2 classes are always contiguous in memory. right *r = new right (); left* l =

Can I use hlookup or similar to keep looking for specific text?

Cary So this is what I want to do. For example, here is the mockup I made to get functionality. On the left, I have people in columns (B/C) and food in rows (2/3). Where people and food meet in the table, I want to search the data on the right to see if their

Can I use hlookup or similar to keep looking for specific text?

Cary So this is what I want to do. For example, here is the mockup I made to get functionality. On the left, I have people in columns (B/C) and food in rows (2/3). Where people and food meet in the table, I want to search the data on the right to see if their