tags:

views:

82

answers:

4

I have a xaml code:

<Grid>
    <WrapPanel>
    <TextBox ></TextBox>
    <Button Content="GetIt" />
    </WrapPanel>
</Grid>

How i can to get all available space for textBox?

i want to do something like that:

|[__________][GetIt]|

+5  A: 

There are a number of ways this can be achieved, including this one:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="Auto" />
  </Grid.ColumnDefinitions>
  <TextBox />
  <Button Grid.Column="1">GetIt</Button>
</Grid>
Steve Greatrex
Thanks! Can i ask another question? How i can glue grid columns.|[__________][GetIt]||...................||...here is another.||.multyline textbox.||...................|
Neir0
Ohh sorry. My comment is lost formatting.I mean that http://img708.imageshack.us/img708/2383/screengw.png
Neir0
Neir0 - You're trying to get the exact effect DockPanel is designed for. You can do the same thing with a Grid, which is much more flexible, but it takes a lot more XAML and requires a lot of maintenance of Row/Column/RowSpan/ColumnSpan values if you need to add or remove items.
John Bowen
+1  A: 

Try this:

<Grid>
    <TextBox HorizontalAlignment="Stretch" Margin="2,2,102,2"></TextBox>
    <Button HorizontalAlignment="Right" Width="100" Content="GetIt" />
</Grid>

Just make the button the desired width and the text box will fill up the rest.


Thanks for the catch; corrected above to properly handle margin on right. This does, however, require you to update the margin when the button width changes. Two columns is a better solution if you plan to change the spacing often. Using the margin is cleaner if you have several controls in the grid and don't want to create nested grids to handle this kind of split.

Dan Bryant
But in this case button is over textbox.Here you can see screenshot http://img214.imageshack.us/img214/918/screeneb.png
Neir0
That's a technique that I'm betting comes out of learning how to do layout in CSS without tables.
Robert Rossney
It's also the kind of code that Blend will 'guess' if you're dropping and resizing controls within a grid.
Dan Bryant
+1  A: 

The simplest way is to use a DockPanel instead of a Grid (the default for LastChildFill is true but I also added it here for clarity):

<DockPanel LastChildFill="True">
  <Button Content="GetIt" DockPanel.Dock="Right" />
  <TextBox ></TextBox>
</DockPanel>
John Bowen
+1  A: 

Here's a way to achieve the layout that you're looking for:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  <Page.Resources>
    <Style TargetType="TextBox">
      <Setter Property="Margin" Value="2"/>
    </Style>
  </Page.Resources>
  <DockPanel>
    <DockPanel DockPanel.Dock="Top">
      <!-- Because the Button is fixed in size, you can divide the row it's 
      in using a DockPanel:  the Button is docked to the right edge, and the
      TextBox fills up the remaining available space. -->
      <Button Margin="2" Padding="2" DockPanel.Dock="Right">GetIt</Button>
      <TextBox />
    </DockPanel>
    <!-- Because the TextBoxes *aren't* fixed in size, you can't use docking,
    as it won't size them.  So put them in a Grid and use star sizing to
    divide the grid's vertical space into two equal parts.   The Grid will
    fill up the remainder of the (outer) DockPanel. -->
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
      </Grid.RowDefinitions>
      <TextBox Grid.Row="0">Another TextBox</TextBox>
      <TextBox Grid.Row="1">Yet another TextBox</TextBox>
    </Grid>
  </DockPanel>
</Page>
Robert Rossney