soufiane labdai
soufiane labdai

Reputation: 31

Bind TextBox Width from ColumnDefinition Width XAML WPF

In my WPF project i want to bind TextBox Width property from ColumnDefinition Width. Now, it's not working !

I used a converter (GridLengthConverter) to convert data to width.

This is my code :

<TextBox Grid.Column="1" Grid.Row="1" Style="{StaticResource SearchWhite}"  Name="tbSearch"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type mic:DataGrid}},
Path=ShowSearchBoxes, Converter={StaticResource visibleConverter}}" Width="{Binding
RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ColumnDefinition}},
Path=Width, Converter={StaticResource gridLengthConverter}, Mode=TwoWay}"/>

gridLengthConverter is GridLengthConverter from assembly : PresentationFramework.

I get an error "Unable to convert data attribut 'Converter'..." (translation from french).

Someone can helps ?

Upvotes: 3

Views: 7419

Answers (2)

ctrl
ctrl

Reputation: 11

If you are binding your text box to width of column 1, all you need to do is leave out the Width property definition and set HorizontalAlignment = "Stretch"

For example:

<Grid>
    <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>

    <TextBox Grid.Column = "1" HorizontalAlignment="Stretch" Height="120" Name="textBox1" />
</Grid>

The textbox now should be of height 120 and width 100.

Upvotes: 1

Grenkin
Grenkin

Reputation: 93

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Name="col1" Width="200"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBox Width="{Binding ElementName=col1, Path=Width}" 
                 Grid.Column="0" Height="20"/>
</Grid>

Upvotes: 4

Related Questions