views:

14676

answers:

6

I have a Window with my user control and I would like to make usercontrol width equals window width. How to do that?

The user control is a horizontal menu and contains a grid with three columns:

<ColumnDefinition Name="LeftSideMenu" Width="433"/>
<ColumnDefinition Name="Middle" Width="*"/>
<ColumnDefinition Name="RightSideMenu" Width="90"/>

That is the reason I want the window width, to stretch the user control to 100% width, with the second column relative.

EDIT:

I am using a grid, there is the code for Window:

<Window x:Class="TCI.Indexer.UI.Operacao"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles"
    Title=" " MinHeight="550" MinWidth="675" Loaded="Load" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized" Focusable="True"
    x:Name="windowOperacao">
    <Canvas x:Name="canv">
        <Grid>
            <tci:Status x:Name="ucStatus"/> <!-- the control which I want to stretch in width -->
        </Grid>
    </Canvas>
</Window>
+1  A: 

What container are you adding the UserControl to? Generally when you add controls to a Grid, they will stretch to fill the available space (unless their row/column is constrained to a certain width).

Andy
A: 

Does setting the HorizontalAlignment to Stretch, and the Width to Auto on the user control achieve the desired results?

Jason Miesionczek
+5  A: 

You need to make sure your usercontrol hasn't set it's width in the usercontrol's xaml file. Just delete the Width="..." from it and you're good to go!

EDIT: This is the code I tested it with:

SOUserAnswerTest.xaml:

<UserControl x:Class="WpfApplication1.SOAnswerTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300">
    <Grid>
     <Grid.ColumnDefinitions>
      <ColumnDefinition Name="LeftSideMenu" Width="100"/>
      <ColumnDefinition Name="Middle" Width="*"/>
      <ColumnDefinition Name="RightSideMenu" Width="90"/>
     </Grid.ColumnDefinitions>
     <TextBlock Grid.Column="0">a</TextBlock>
     <TextBlock Grid.Column="1">b</TextBlock>
     <TextBlock Grid.Column="2">c</TextBlock>
    </Grid>
</UserControl>

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="415">
    <Grid>

     <local:SOAnswerTest Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="2"/>
    </Grid>
</Window>
Arjan Einbu
Thanks for the attention, Arjan. It works fine here too, but my window has no Width="415" setted, because it is designed to be full screen. When I set width, it works well.
Victor Rodrigues
+4  A: 

Is the Canvas crucial in your window? If not, try removing it and keep the Grid as the main panel. Canvas has no size unless specified, while a Grid normally takes up all available space. Inside the Canvas, the Grid will have no available space.

Bubblewrap
+2  A: 

The Canvas in WPF doesn't provide much automatic layout support. I try to steer clear of them for this reason (HorizontalAlignment and VerticalAlignment don't work as expected), but I got your code to work with these minor modifications (binding the Width and Height of the control to the canvas's ActualWidth/ActualHeight).

<Window x:Class="TCI.Indexer.UI.Operacao"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles"
Title=" " MinHeight="550" MinWidth="675" Loaded="Load" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized" Focusable="True"
x:Name="windowOperacao">
<Canvas x:Name="canv">
    <Grid>
        <tci:Status x:Name="ucStatus" Width="{Binding ElementName=canv, Path=ActualWidth}" Height="{Binding ElementName=canv, Path=ActualHeight}"/> <!-- the control which I want to stretch in width -->
    </Grid>
</Canvas>

The Canvas is the problem here. If you're not actually utilizing the features the canvas offers in terms of layout or Z-Order "squashing" (think of the flatten command in PhotoShop), I would consider using a control like a Grid instead so you don't end up having to learn the quirks of a control that works differently than you have come to expect with WPF.

Mick
Thanks, binding to ActualWidth helped me in a project where I had a custom UserControl in a StackPanel. The StackPanel used HorizontalAlignement=Stretch and took the whole width, but the usercontrol remained at 0 width. Binding the user control to its parent stackpanel ActualWidth solved it for me.
PRINCESS FLUFF
+1  A: 

Instead use Weight and Height in user controls, use MinHeight and MinWeight. Then you can configure the UC well, and will be able to stretch inside other window.

Well, as Im seeing in WPF Microsoft made a re-thinking about windows properties and behaviors, but so far, I didn't miss anything from olds windows forms, in WPF the controls are there, but in a new point of view.

Leonardo Nowaczyk