locked
How to change textbox width in datagrid?

    Question

  • Hi,

    I have a datagrid that contains among other things a textbox that lives inside a resizable window. When I resize the wdinow I want the textbox to resize with it.

    My xaml looks as follows:

     <data:DataGrid.Columns>
      <data:DataGridTemplateColumn x:Name="ChatColumn" IsReadOnly="True">
       <data:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
         <StackPanel>
          <StackPanel Orientation="Horizontal">
           <TextBlock Text="{Binding From}" HorizontalAlignment="Left"/>
           <TextBlock Text=" said at "/>
           <TextBlock Text="{Binding Date, Converter={StaticResource ExtractTime}}" HorizontalAlignment="Right"/>
          </StackPanel>
          <TextBox x:Name="TextMessage" Foreground="{Binding TextColour}" Text="{Binding Text}" TextWrapping="Wrap" MaxWidth="{Binding Width, Mode=OneWay, ElementName=ChatMessages}" />
         </StackPanel>
        </DataTemplate>
       </data:DataGridTemplateColumn.CellTemplate>
      </data:DataGridTemplateColumn>
     </data:DataGrid.Columns>

    The problem line is:

    <TextBox x:Name="TextMessage" Foreground="{Binding TextColour}" Text="{Binding Text}" TextWrapping="Wrap" MaxWidth="{Binding Width, Mode=OneWay, ElementName=ChatMessages}" />

    I am attempting to bind the width to another controls width that I am dynamically changing the width of. The problem is that the width is not being reflected in the TextBox. Any ideas as to why its not working, or any ideas for a workaround?

    Thanks

    Mat
     

     

    Wednesday, September 23, 2009 6:48 PM

Answers

  •  I assume that the textbox appears smaller in width than the column it is contained in? If not than you must also resize the column width.

    HTH.

    Friday, September 25, 2009 2:31 AM

All replies

  •  Any reason why you're using MaxWidth instead of Width?

    Also try and bind to ActualWidth, as it indicates, well, the actual width of the control after a measure has occured.

    HTH.

    Thursday, September 24, 2009 2:27 AM
  • I've tried Width and MaxWidth, neither of them appear to bind. I've also tried binding to the ActualWidth property and still no joy.

    I took the textbox out of the DataGrid and put it into a ListBox and the binding works fine, so it must be something to do with the DataGrid, maybe a bug?

    Thursday, September 24, 2009 6:33 AM
  •  I assume that the textbox appears smaller in width than the column it is contained in? If not than you must also resize the column width.

    HTH.

    Friday, September 25, 2009 2:31 AM
  • Thanks many times over for steering me in the right direction. Adjusting the columns MinWidth and MaxWidth to match the text boxes width fixed the problem

    Saturday, September 26, 2009 6:17 AM