Autosizing ReportViewer in WPF? - wpf

I am designing a RDLC report in WPF. Naturally I am using WindowsFormsHost to host a ReportViewer object to display the report. However, since the length of the report, specifically, the height, of the report varies each time the report is calculated and rendered depending on the actual content of the report, I want the control can adjust the size of the ReportViewer object. How can I do that? Currently I set the height of WindowsForsHost object as Auto. But that only takes effect at the first time when the WPF page is loaded, when no report is actually displayed(We need the user to input some parameters for the report and then click to calculate the report). And whenever the new report is displayed, the size of the object is not changed any more.
Thanks!

I'm not sure I got your Problem right. Maybe this helps:
You can bind the Width and Height of a control to the width and height of an other control.
For Example by defining the type of the first parent element that has the right width, here a grid:
Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=Grid}, Path=ActualWidth"}
or by the name of the element:
Width="{Binding ElementName=MyElement, Path=ActualWidth}"
ActualWidth is the current width of the element, if the width of MyElement changes, the width of the bound element changes too. Same for the Height property.

Related

How do I solve this layout scenario involving a collapsible region?

I have the following layout problem best explained here (apologies for the crude illustration)
Assume each control is implemented as a separate user control
I want behavior such that expanding the top right (green) control will render into view an expanded region wider than the initial (green) control's width and appear directly beneath it, with a width equal to the 2 controls in the first row (eg colspan of 2) while pushing down all the content beneath it
Assume initially there is a grid with 2 rows ..first row is some control and secondly the expandable control - the second row can have anything that will fill a colspan of 2 ..could be another layout container or user control with anything inside it etc.
If the green expandable control was a user control, what would I need to do to obtain this behavior I'm describing?
I'm thinking I need to set the Grid.Row and Colspan attached properties of the container representing the expanded content(?) from inside the user control to the layout grid the user control is inside? (but then how would I set the attached property of a control inside the user control to a layout container outside the user control - especially if the expanded content is itself inside a grid within the user control).
And even if I could do this, this would be very fragile since it would only work if the control was in a grid layout container.
Any ideas? Doesn't have to use a grid but that seems to be the natural / obvious way to solve it?
I had a similar layout a while ago. This worked for me:
On the green control, create dependency properties for the sizes you need. In this case, I expect the full width (lower rect) and the narrow width (upper rect).
In the top-level container, you create a grid for measuring the sizes. So you'll get something like this:
<Grid x:Name="fullSize">
<ColumnDefinition/>
<ColumnDefinition x:Name="halfSize"/>
</Grid>
Here you can divide the width over the columns in the appropriate way.
- Then, bind the widths measured with this grid
<foo:GreenControl
FullWidth={Binding ElementName=fullSize, Path=ActualWidth}
NarrowWidth={Binding ElementName=halfSize, Path=ActualWidth}
/>
Now the GreenControl can set its width to whatever it needs to be, e.g. in the handler for the value update. In my case I set bindings on the elements in the XAML to the dependency props.

Auto resizing wpf elements with scroll bars (Rich text box, list box) vb

I'm having a problem where I have elements such as Listboxes and Rich Text boxes that I want to set to size automatically in xaml according to the size of the window, but I only want it to resize to the size of the window and then put scrollbars if the content is any bigger than that.
Unfortunately, the only way I can get scroll bars to work is if I set a specific height of the listbox/rich text box (which does not work because I want it to automatically resize to the height of the grid that it is contained within, which is generally the height of the window (auto).
Any help would be greatly appreciated.
You do not need to use fixed values for Width and Height - you should rather specify a minimum width/height for your controls using the MinWidth and MinHeight properties. Then try a layout similar to this:
<Window>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid>
<ListBox MinWidth="500" MinHeight="250"/>
<!-- any other controls... -->
</Grid>
</ScrollViewer>
</Window>
The Grid generally uses all the space it gets if its alignment properties are set to Stretch and if at least one row/column is set to be star-sized. In this case, there are only one row and one column, implicitly created, both star-sized by default.
To make the ScrollViewer work, you need to somehow set a minimum size of your content controls because otherwise the ScrollViewer does not know when to activate the ScrollBars. In the example above, I have done that using the MinHeight and MinWidth properties of the ListBox, but you could also set these properties on the Grid's RowDefinitions and/or ColumnDefinitions.
Now, if you resize the window, so that the Width becomes smaller than 500, you will see that scrollbars will appear. Just check it out.

VisualBrush Visual Refresh Problem

I have a grid with two rows.
In the first row, I have a rectangle, fixed width and height, whose fill is a VisualBrush whose Visual binds to a tabcontrol's selecteditem.content, which resides in the grid's second row.
All works well - the rectangle displays a scaled down version of the SelectedItem's content in the TabControl. The problem is if I remove the current TabItem and add a different one, the preview doesn't change.
Any thoughts?
Chris

How to determine width of a windows forms controls whose width was increased because it is Anchored?

I currently have a Textbox on a Windows Forms, and I need to dynamically add a PictureBox box control at the right of the Textbox.
I create the PictureBox programmatically and I when setting the location of the PictureBox, i'm setting like this:
pBox.Location = new Point(tbControl.Location.X + ctrl.Width, ctrl.Location.Y);
So i'm setting the picture box to be located at the X location of the textbox PLUS the width of the textbox. However, since the textbox has an anchor property set to right, its width increases to fill the space between itself and the form border.
Problem is, that even though the textbox's width is visually bigger than the actual value of Textbox.Width. the Width property is not taking into account the extra width of being anchored.
I already tried properties like Textbox.Bounds.Width, Textbox.ClientSize.Width, Textbox.DisplayRectangle.Width, etc. with no luck. All of those properties return the original Width of the control without taking into account the resized width due to the Anchor property.
Does anyone know how I can determine the real size of the textbox? Thank you
The Width property always tracks the current width of a control, whether it is anchored or not. However, the TextBox is going to grow when you make the container larger and that will make it overlap the PictureBox. You have to anchor the PB to the right as well.
These should be returning the adjusted size. Either you are referring to the wrong textbox, or you are doing the query before the size has actually changed.

Stretching a WPF Canvas Horizontally

How do I make a Canvas stretch fully horizontally with variable width? This is the parent Canvas, so it has no parents, only children.
XAML Source: it displays in blend
http://resopollution.com/xaml.txt
Use a Grid as the top level element in your UI - it'll stretch to fill its container. Then put a Canvas with HorizontalAlignment="Stretch" inside the Grid and it'll behave the way you want.
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Background="Blue"/>
</Grid>
That worked for me. The key is your top level UI element. While a Grid fills all available space by default, Canvases take up only as much room as their contents demand.
I'm guessing you've tried
canvas.HorizontalAlignment = HorizontalAlignment.Stretch
If this doesn't work, then what you could do is bind the Width and Height properties of the canvas to the ActualWidth and ActualHeight properties of the containing window.
You could use a dock panel to get it to fill the available width. The last item in a dock panel list of controls is automatically stretched to fill the remaining space.
<DockPanel>
<Canvas />
</DockPanel>
The canvas should do this automatically, unless you are manually setting the height and/or width. What kind of control are you trying to place the canvas on? Can you post your code?
The problem is that you're specifying the Height and Width. Without these properties, the control may appear to vanish in the designer, but it should size appropriately when you insert the canvas into another control.
If I recall correctly, the next version of WPF will have 'DesignWidth' and 'DesignHeight' properties that allow you to show the control in the designer with a given size without effecting it's measurement when inserted into other controls.

Resources