Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF - WrapPanel with Fill

Rate me:
Please Sign up or sign in to vote.
4.28/5 (12 votes)
22 May 2015CPOL2 min read 28.6K   755   14   4
A WPF WrapPanel that can fill empty space on any lines with any control(s)

Introduction

I realized than many people need the same thing as me, a WrapPanel that can fill empty space to the right (Orientation=Horizontal) with one or more of its child controls. I decided to write a re-usable control that does the job in both orientations.

The code contains a little demo where you can easily see if it fits your need.

Note: I really appreciate feedback. If you ever do not like the code, please let me know why. I hope it could help anybody.

Screenshot of the Sample Code

Image 1

Background

There are a few questions on StackOverflow but no real easy solutions that work when there is more than one line. Also, I wanted to do a Control that I could easily re-use anywhere. I started from the Microsoft code and modified it in order to give the required behavior.

Using the Code

You can either use the DLL or just copy the source code (only one .cs file) in your own library.

Usage is as follows:

XML
<Window x:Class="WpfWrapPanelWithFillTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wrapPanelWithFill="clr-namespace:WrapPanelWithFill;assembly=WrapPanelWithFill"
        Title="MainWindow" Height="400" Width="800">

  <wrapPanelWithFill:WrapPanelFill Grid.Row="2" Grid.Column="6" Orientation="Vertical">
    <TextBlock Text="Path: " TextWrapping="Wrap"></TextBlock>
    <TextBox MinWidth="120" wrapPanelWithFill:WrapPanelFill.UseToFill="True">*</TextBox>
    <Button>Browse...</Button>
  </wrapPanelWithFill:WrapPanelFill>

</Window>

Limitation (way to improve)

  • Taking in account MaxWidth (or MaxHeight when Orientation is vertical) for controls defined as filler.
  • Fill width is always the same for every child (when more child are defined as "Filler". It would be nice to have the same "proportion" defintion like the one used in "Grid" using "GridLength". For example "Width" of RowDefinition.
  • Add the HorizontalContentAlignement and VerticalContentAlignement to make the control more complete. It is usefull when we need to align control on the right or center instead of the Left. I found a nice solution to that an answer from DTig at StackOverflow.

Ideally, its a combinaison of each improvement in one solution that would be nice to have.

History

  • 2015-05-12, First version
  • 2015-05-13, Made code a little bit cleaner, fixed some mistakes in tip and added a screenshot
  • 2015-05-22, Clarify limitions. Improve a little bit the text.

License

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


Written By
Software Developer IREQ - Hydro-Quebec
Canada Canada
Hi,

I'm a french Canadian software developer.
Work at Hydro-Quebec / IREQ.
Live in Sainte-Julie, Quebec, Canada (South shore of Montreal)

WebPage: http://www.ericouellet.com
GMail: ericouellet2

Salut!

Comments and Discussions

 
PraiseFantastic Pin
Member PAWELWOJDA12-Jun-20 5:46
professionalMember PAWELWOJDA12-Jun-20 5:46 
Praisethanks Pin
Member 1241236114-Jul-19 21:54
Member 1241236114-Jul-19 21:54 
PraiseJust thank you Pin
John Smith27-Dec-15 10:58
John Smith27-Dec-15 10:58 
GeneralRe: Just thank you Pin
Eric Ouellet8-Jan-16 10:57
professionalEric Ouellet8-Jan-16 10:57 
Thank you!!! Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.