oschepkov
oschepkov

Reputation: 75

Binding inside DataTemplate grid property to its column definitions

What I need is to guaranty that all my columns aren't gonna be stretched by its content controls when they are bigger. Now it does. I found a solution here enter link description here. It works, but I'm wondering if it's possible to bind to item template Grid Width itself somehow instead of binding to the ListBox Width? And if it's possible is it proper way to go in terms of performance?

<telerik:RadListBox.ItemTemplate>
                <DataTemplate>
                    <Grid> <-- need to bind this Grid Width
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" /> <-- to these columns width for further calculations
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

Upvotes: 0

Views: 166

Answers (1)

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

You can name the parent Grid and bind to it like:

<Grid x:Name="ParentGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding Path=ActualWidth,
                                          ElementName=ParentGrid,
                                          Converter={StaticResource YourConverter}"/>
        <!-- ... -->
    </Grid.ColumnDefinitions>
    <!-- ... -->
</Grid>

An other way is to bind the Grid as parent like:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="{Binding Path=ActualWidth,
                                          RelativeSource={RelativeSource AncestorType={x:Type Grid}},
                                          Converter={StaticResource YourConverter}"/>
        <!-- ... -->
    </Grid.ColumnDefinitions>
    <!-- ... -->
</Grid>

Upvotes: 1

Related Questions