Hi,
I am using:
<Style TargetType="{x:Type igEditors:XamTextEditor}"> <Setter Property="TextWrapping" Value="Wrap"/></Style>
to wrap a cell. However I have set the row height to 25. I need to determine if wrapping occurred on each record and if so, set that record's height to 50. Is this possible?
Like TextBox, XamTextEditor contains methods for determining how many lines of text are in the editor. The method LineCount will become equal to 2 as soon as text wraps to a second line. You can handle an event (such as TextChanged) and resize the editor once LineCount is greater than 1.
I cannot find this "LineCount" method you mention in my current version of XamTextEditor (NetAdvantage for WPF 2007 Vol. 2). Can you give me a hint on what it's actually called?
A WPF TextBox is part of the architecture behind the XamTextEditor. The TextBox manages the rudimentary text editing. To access the underlying TextBox control you either need to use the VisualTreeHelper or you can access it by capturing a TextBox event.
The following is a shortcut for accessing the TextBox when the user makes an edit. The following resource will call an event handler for all TextBoxes found in the Window:
<Window.Resources> <Style TargetType="{x:Type TextBox}"> <EventSetter Event="TextChanged" Handler="OnTextChanged"/> </Style></Window.Resources>
<igEditors:XamTextEditor/>
In the code-behind, you will have access to the TextBox
private void OnTextChanged(object sender, TextChangedEventArgs e){ if (sender is TextBox) { TextBox box = sender as TextBox; if (box.Height != 50 && box.LineCount > 1) box.Height = 50; }}
very fast solution:
<igEditors:XamTextEditor >
<igEditors:XamTextEditor.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="AcceptsReturn" Value="True"/>
</Style>
</igEditors:XamTextEditor.Resources>
</igEditors:XamTextEditor>
The TextBox is part of the infrastructure of the XamTextBox. Thus the name given the TextBox is used by the template.
If you want to use the shortcut I added for only one XamTextBox, assign the style to just the XamTextBox:
<igEditors:XamTextEditor> <igEditors:XamTextEditor.Resources> <Style TargetType="{x:Type TextBox}"> <EventSetter Event="TextChanged" Handler="OnTextChanged"/> </Style> </igEditors:XamTextEditor.Resources></igEditors:XamTextEditor>
Curtis, I understand that using your above example if I had multiple controls of type "XamTextEditor", that the OnTextChanged event would fire for all of them. Is this right?
If I wanted to change the height of only one XamTextEditor, how would I do it?
I tried the following, where "txtXamTextEditor" was the name of the instance of the XamTextEditor I'd like this code to execute on:
Private Sub OnTextChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Dim iLineCount As Integer = 0 Dim oTextBox As TextBox If TypeOf sender Is TextBox Then oTextBox = CType(sender, TextBox) If oTextBox.Name = txtXamTextEditor.Name Then iLineCount = oTextBox.LineCount Select Case iLineCount Case 1 oTextBox.Height = 25 Case 2 oTextBox.Height = 50 Case Else ' I'm doing something else, it doesn't matter End Select End If End If End Sub
Private Sub OnTextChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Dim iLineCount As Integer = 0 Dim oTextBox As TextBox
If TypeOf sender Is TextBox Then oTextBox = CType(sender, TextBox) If oTextBox.Name = txtXamTextEditor.Name Then iLineCount = oTextBox.LineCount Select Case iLineCount Case 1 oTextBox.Height = 25 Case 2 oTextBox.Height = 50 Case Else ' I'm doing something else, it doesn't matter End Select End If End If End Sub
But it didn't work since oTextBox.Name was "PART_FocusSite", the name of the textbox inside the "Edit Control Template".
Thanks Curtis - that helps! You also helped me answer my own question in the thread Allowing 'Carriage Return' in a multiline XamTextEditor