Arrie
Arrie

Reputation: 1337

Get text from DataGridTextColumn with the Textblox style in code behind

i am currently supporting a project and i have received a job to change a validation on a datagridcell to make the text-block's input they have inserted only be 50 characters.

i have now went and got the datagrid gotfocus() event, and check for the headers of each DataGridTextColumn, and if the correct one gets focus , i create a Key_Down event for the column.

In The Key_Down event i wan to test the length of the current string, if it is more than 50 i want the textbox to not add anymore to the string. but my problem is , i do not get the text from the DataGridTextColumn . here is my code :

XAML

<DataGrid Grid.Column="3" GotFocus="transitRouteParticularsGrid_GotFocus" AutoGenerateColumns="False" x:Name="transitRouteParticularsGrid" Grid.ColumnSpan="9" Grid.Row="30" Grid.RowSpan="6" CanUserResizeColumns="False" CanUserSortColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False">

<DataGridTextColumn x:Name="cmbDestination" Header="Destination" EditingElementStyle="{StaticResource errorStyle}" Width="200" Binding="{Binding CustomDestination, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">                        
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="TextAlignment" Value="Center" />

                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

   </DataGrid>

Code behind I have so far

private void transitRouteParticularsGrid_GotFocus(object sender, RoutedEventArgs e)
        {
            if (e.OriginalSource.GetType() == typeof(DataGridCell))
            {
                DataGridCell ChosenItem = (DataGridCell)e.OriginalSource;

                if ((ChosenItem.Column.Header.ToString() == "Destination") && (Routevalue == "CUSTOM"))
                {
                    ChosenItem.KeyDown += ChosenItem_KeyDown;
                }
            }
        }


 void ChosenItem_KeyDown(object sender, KeyEventArgs e)
        {
            string curentText = "";
            int maxlen = 50;
            DataGridCell griddestination = (DataGridCell)sender;

            if (griddestination.Column.GetType() == typeof(DataGridTextColumn))
            {
                //i have no idea what should be here???
                //any other solution would also be appreciated

                var DestinationColumn = griddestination.Column;

                //Check for length

                if (curentText.Length > 50)
                {
                   //Do whatever to text
                }
                else
                {
                   // destination.Text = curentText;
                }
            }
            else
            {
                e.Handled = true;
            }         
        }

IMAGE Runtime how the datagrid looks

so i need to know how to get the text i put in for this column?

any help would be awesome :)

thanks.

Upvotes: 0

Views: 1644

Answers (1)

pushpraj
pushpraj

Reputation: 13679

You can get the the visual tree for the cell using DataGridCell.Content property, navigating the same depends on the errorStyle template defined in EditingElementStyle="{StaticResource errorStyle}"

assuming that it is based on the TextBox here is how you can get the textbox

TextBox cellTextbox = (TextBox)griddestination.Content;

now you may use the same for getting text value or manipulation.

above will only work if the cell is in edit mode, otherwise cell content is a TextBlock as defined by ElementStyle

for safe side you may write the same as

TextBox cellTextbox = griddestination.Content as TextBox;
if(cellTextbox != null)
{
   //your logic
}

Upvotes: 1

Related Questions