Align control to right - xaml - c#

I have Grid with 3 columns.
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="215"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
In first column I have image.
In second Stack Panel.
In third I have my user control and I want to make it horizontal right, but it's align to left.
This is code for my third column
<local:MyControl
Grid.Column="2"
HorizontalAlignment="Right" />

There is no width defined for user control hence it will expand to fit all the width that is available in that column.
Note for 3rd Column <ColumnDefinition Width="*" />

If in your user control, you have designed the layout inside a grid, and the alignment of the elements inside the grid (possibly "LayoutRoot") is left, then the control will take up whole of your third column and your control wilol appear as if it is left aligned, even though you have aligned it to the right.
So, even though your control is aligned to the right, the elements inside the control may be aligned to the left and if the "LayoutRoot" of the control is a grid then maybe it is the cause of your problem.

Related

Grid column width calculation Windows 10 when ColumnDefinition Width="*"

In Windows 8.1 I have a working Xaml Grid code that creates three columns two on the very left and right and one in the middle that fills the space.
<Grid x:Name="HeaderGrid" DataContext="{Binding}" MinHeight="40" Width="200"
HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
...
</Grid>
In Windows 10 the third column does not show up. By changing the Width of the second column to "Auto" I can see all the columns but that's not the UI design that I need.
It sounds to me like maybe the parent of the Grid is wider than you're expecting so the Grid is getting clipped on the right. Can I see more of the Xaml that holds the Grid? You can also check by setting the Grid's HorizontalAlignment to Right to try and figure out where the right edge of its parent is. If you're using VS2015RC you can use the "Live Visual Tree" tool while F5 debugging under Debug->Windows->Live Visual Tree.

Cannot get grid splitter to work the way I would like within 3 column grid in WPF

I have the following grid using WPF and a few third party controls (Telerik):
<Grid x:Name="grid2" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="150"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*" MinWidth='80'/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="26"/>
<RowDefinition/>
</Grid.RowDefinitions>
<telerik:RadBreadcrumb Grid.ColumnSpan="3" Header="RadBreadcrumb"/>
<telerik:RadTreeView Grid.Row="1"/>
</Grid>
<GridSplitter Grid.Column="1" HorizontalAlignment="Center" Width="5" Height="auto"/>
<telerik:RadTransitionControl x:Name="Trans" Grid.Column="2" Width="auto"/>
</Grid>
Currently I am trying to make it so that the grid in column 0 (on the left side) will not re-size while re-sizing the window. I was able to achieve this effect by setting the width of column "0" to either a static 150 or auto. However, when I did this, the grid splitter no longer respected the minimum width of column 2 and allowed it to drag off the screen. By setting both column 0 and 2 to a width of star (the way I have it in the code now) the min widths are respected (the columns can't shrink past the 150 and 80 defined) but they don't re-size properly (the left column re-sizes in proportion to the right column). I would like to somehow get both the resize and the gridsplitter to work at the same time. Let me know if I wasn't clear enough or if more input is needed.
So, this isn't exactly what I was looking for, but it's doing the job for me:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MinWidth="150" MaxWidth="1000"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
I set the MaxWidth of my left hand column so that you could no longer drag the splitter past the point of no return on the right side. This solution also allows me to set the first column's width to auto (which makes the re-sizing work the way I intended as well). Unless you have a monitor smaller than 1000px wide (which in the environment I'm working in won't happen) this solution will work just using XAML. Again, I'm a little disappointed with this solution but its at least working in a simplistic manner.

Using "pure" xaml layout, is it possible to tell a container to not resize to it's children?

I have a XAML control that's very wide. It affects the size of it's parent, thus changing layout I don't want it to change. I don't mind if the control will be clipped, or whatever - but I want the following:
I want the control to be the width of it's parent and
I don't want it to otherwise affect the width of its parent.
I know I can achieve this by binding Width to ActualWidth of the parent control I am interested in. However, is there some panel or something that will take its size from its parent while ignoring it's children.
Because this is inside a Grid that has star sized columns, just putting it inside a ScrollViewer does not help - the ScrollViewer still grows to fill the grid.
In the following example, I want the button to govern the width of the middle column and the textblock to take, at most, the amount of space the Button does:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Content="asdsadsad" Grid.Row="1" Grid.Column="1"/>
<TextBlock IsHitTestVisible="True" Opacity="0" Grid.Row="1" Grid.Column="1" Text="sdfsdfdsfdfhfkjahslkjfhsafhsakjhflkjsahflkjsahdflkjhsadfsdfkgljdsfkgjfdsjgdshf" />
</Grid>
You can put the TextBlock in a Canvas and then clip the canvas to its bounds:
<Canvas Grid.Row="1" Grid.Column="1" ClipToBounds="True">
<TextBlock IsHitTestVisible="True" Opacity="0.5" Text="sdfsdfdsfdfhfkjahslkjfhsafhsakjhflkjsahflkjsahdflkjhsadfsdfkgljdsfkgjfdsjgdshf" />
</Canvas>
if that's what you mean.
I saw you've already accepted the other answer, but if you're only referring to text blocks, couldn't you just use TextTrimming?
If that's not the case, a simple Panel subclass would be better than the canvas as you could explicitly ignore the children's size when determining the size you need. You'd still need to measure the children. You'd just ignore the 'DesiredSize' of them when determining the panel's size in the MeasureOverride. Then in ArangeOverride, it would respect the maximum size you specified in MeasureOverride.
The advantage of that is there too textboxes or any control for that matter would still properly size and layout as children, but but they would never affect the panel's size.
Here's the MeasureOverride code you'd need. Then in ArrangeOverride, you just lay the children out as you want, deciding to consider clipping or constraining to the panel as you see fit. (Custom panels are one of my favorite WPF things.)
protected override Size MeasureOverride(Size availableSize)
{
// You still measure the children. You just don't use them when determining the size of the panel.
foreach(FrameworkElement childElement in this.Children)
{
if(childElement.Visibility != Visibility.Collapsed)
childElement.Measure(availableSize);
}
return new Size(
Double.IsPositiveInfinity(availableSize.Width) ? 0 : availableSize.Width,
Double.IsPositiveInfinity(availableSize.Height) ? 0 : availableSize.Height);
}

Force control to stay beside another

I am in the process of completing a fault logger for my company.
Unfortunately, not all people have the same screen resolution (ranging from 800 - 1080), I need to create controls that can change size based on the window size.
So far, I have been able to force controls to change their dimensions based on window dimensions, but I am faced with a challenge of them overlapping after a certain size, as shown below:
Is there a way to force the TextBox and GroupBox to float just left of the DataGrid, so it will move right based on the width of the grid?
Grid columns will solve the overlap problem but you might want to consider using a GridSplitter control, for example:
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto" />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<GridSplitter Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Background="Black"
ShowsPreview="True"
Width="5"
/>
References & useful pages:
"GridSplitter Class (System.Windows.Controls)" [msdn.microsoft.com]
Bing Search: 'gridsplitter'

Silverlight Grid resize issue

I am developing a Silverlight web app and am working now on a child window which is separated in three columns. The left is for messages which will not be displayed always, and two (middle and right) which will be displayed always.
My question is: How can I make the right and middle column use up all the space if the left column doesnt need to show any data? So if for example my child window is 100 in width every of the three should be 33.333 or if the left one doesn't need to be displayed then the other two are both 50. Is there any way without messing in code behind?
Edit: The child window looks like this
<controls:ChildWindow ...
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid Name="msgGrid" Grid.Column = 0/>
<Grid Grid.Column = 1/>
<Grid Grid.Column = 2/>
</controls:ChildWindow>
I am passing a bool value (showMessageToUser) into the constructor of the child window and based on that it should be determined what to do
Make the first column "Auto" size and the second two columns 1* (or just *) in width.
The first column will collapse to its contents (which presumably you will show/hide) and the second two will take up 50% each as the * sizing is just a ratio.
Put a name container (another grid?) in the first column and control its visible flag. The other 2 columns will take care of themselves.
<controls:ChildWindow ...
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid x:Name="msgGrid1" Grid.Column=0 Width="100"/>
<Grid Grid.Column=1/>
<Grid Grid.Column=2/>
</controls:ChildWindow>
Control the visibility of msGrid1 to get the effect you wanted. If you also want user control of the the width if column 0 (e.g. with a GridSplitter) you will need code-behind as the two features do not work together.

Categories

Resources