Page view counter
datagrid - template column - strange behavior
Last post 05-09-2008 11:21 AM by igal. 5 replies.
Sort Posts:
05-04-2008 7:09 PM
datagrid - template column - strange behavior

Here is definition of one of template columns defined as follows:

<my:DataGridTemplateColumn Header="Value">

<my:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<TextBlock Text="{Binding Value, Mode=TwoWay}" FontFamily="Trebuchet MS" FontSize="11" Margin="5,4,5,4"/>

</DataTemplate>

</my:DataGridTemplateColumn.CellTemplate>

<my:DataGridTemplateColumn.CellEditingTemplate>

<DataTemplate>

<TextBox Text="{Binding Value, Mode=TwoWay}" FontFamily="Trebuchet MS" FontSize="11" Margin="3,2,3,2"

AcceptsReturn="True"

/>

</DataTemplate>

</my:DataGridTemplateColumn.CellEditingTemplate>

</my:DataGridTemplateColumn>

1. On mouse double click on cell of TextBox I can edit text. Despite the fact that AcceptReturn defined as True when I press Enter focus goes to the following field.

2. When I edit the TextBox cell the text in TextBlock and in binding List doesn't change, but when I edit text for the second time everything is OK (both TextBlock and List reflects changes I made.

 

I feel that an answer is simple, but still can't find it.

Thanks in advance,

Igal.

igal

Loading...
Joined on 10-12-2007
Posts 26
05-06-2008 1:09 AM
Marked as Answer
Re: datagrid - template column - strange behavior

Hello, the first problem is caused by an event handler in DataGrid.

private void DataGrid_KeyDown(object sender, KeyEventArgs e)

{

if (!e.Handled)

{

e.Handled = ProcessDataGridKey(e);

}

}

As you can see, DataGrid itself will handle the KeyDown event. You may want to handle the TextBox's KeyDown event and set e.Handled to true.

private void TextBox_KeyDown(object sender, KeyEventArgs e)

{

if (e.Key == Key.Enter)

{

e.Handled =
true;

}

}

Unfortunately this will prevent the TextBox to create a new line. Currently there's no good work around. The only solution I can think out is:

if (e.Key == Key.Enter)

{

((
TextBox)sender).Text += "\n";

e.Handled = true;

}

This will create a new line, but it will move the caret to the beginning of the TextBox. Unfortunately currently there's no way to control the caret of a TextBox...

For the second problem, it works fine for me. After the TextBox loses focus, the TextBlock is updated with the new text.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,688
05-09-2008 8:17 AM
Re: datagrid - template column - strange behavior

the problem is that e.Handled prevents ANY key from beeing entered into the TextBox. I worked aorund this already by showing a non data grid text box which I just lay above the text box when selected.

However your idea might work as one CAN SET the caret via TextBox.Selection. So most likely contructing the text box text manually with the key events might work. 

Joe Robe

joer00

Loading...
Joined on 07-05-2006
Posts 113
05-09-2008 9:37 AM
Re: datagrid - template column - strange behavior
How you put non-datagrid TextBox just above the datagrid textbox?
igal

Loading...
Joined on 10-12-2007
Posts 26
05-09-2008 10:10 AM
Marked as Answer
Re: datagrid - template column - strange behavior

 XAML:

<Grid>

..your row column defintions 

        <TextBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2" x:Name="AdditionalPersonDescription" Canvas.ZIndex="1" AcceptsReturn="true" Width="100" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" Style="{StaticResource TextBoxDescription}" KeyDown="AdditionalPersonDescription_KeyDown" Visibility="Collapsed"/>
<DataGrid....

CodeBehind:

        void DataGridAdditionalPersons_BeginningCellEdit(object sender, DataGridCellEditingCancelEventArgs e)
        {
            currentRow = e.Row;
            currentColumn = e.Column;
            FrameworkElement _Element = e.Column.GetElement(currentRow);
            if (currentColumn.Index == 0)
            {
                HotelRoomPriceAdditionalPerson _Rate = _Element.DataContext as HotelRoomPriceAdditionalPerson;
                Point _Point=Utilities.GetElementPosition(_Element, Utilities.Corner.LeftTop);
                Thickness _Margin = new Thickness(_Point.X, _Point.Y, 0, 0);
                Page.AdditionalPersonDescriptionTextBox.SetValue(TextBox.MarginProperty, _Margin);
                if(_Rate.Description!=null)
                    Page.AdditionalPersonDescriptionTextBox.Text = _Rate.Description;
                Page.AdditionalPersonDescriptionTextBox.Visibility = Visibility.Visible;
            }
        }

Note: In my case the text box sits on a UserControl shown on edit in a Main Data Grid. Thats why I added the edit text box as a static member of the main class. 

Joe Robe

joer00

Loading...
Joined on 07-05-2006
Posts 113
05-09-2008 11:21 AM
Re: datagrid - template column - strange behavior
Thanks a lot.I will try this approach in a couple of days.
igal

Loading...
Joined on 10-12-2007
Posts 26
Microsoft Communities