I have created a datagrid of multiple rows and columns. One of the columns is a list of field sizes that the user can change.
I am checking the new value against the old value and if the new value is less then the old value I am telling the user this is not valid and then I want to put the old value back and reset the focus to that cell.
I have this line in my LostFocus event:
System.Windows.Controls.TextBox tbNewSize =
(System.Windows.Controls.TextBox)dtgCell.Content;
When I click on the cell, the LostFocus event is called and works fine. However when i try to refocus to the cell I get an error saying
"Unable to cast object of type 'System.Windows.Controls.TextBlock' to type 'System.Windows.Controls.TextBox'."
How do I correct this issue?
Here is my XAML code:
<DataGrid HeadersVisibility="Column" Name="dtGrid" Loaded="GridLoaded" AutoGenerateColumns="False" IsReadOnly="False" VirtualizingPanel.IsVirtualizing="False" Height="365" Width="530" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="54,74,0,0" BorderThickness="1,1,0,1" BorderBrush="Black">
<DataGrid.Columns>
<DataGridTextColumn Header="Field" Binding="{Binding Field, Mode=TwoWay}" Width="209" IsReadOnly="True" />
<DataGridTextColumn Header="Size" Binding="{Binding Size, Mode=TwoWay}" Width="89"/>
<DataGridCheckBoxColumn Header="Right Justify" Binding="{Binding RightJustify, Mode=TwoWay}" Width="55" />
<DataGridCheckBoxColumn Header="Left Justify" Binding="{Binding LeftJustify, Mode=TwoWay}" Width="55" />
<DataGridCheckBoxColumn Header="Left Zero Fill" Binding="{Binding LeftZeroFill, Mode=TwoWay}" Width="55" />
<DataGridCheckBoxColumn Header="Right Zero Fill" Binding="{Binding RightZeroFill, Mode=TwoWay}" Width="65" />
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="#FF9DF3D6" />
<Setter Property="Foreground" Value="#000000" />
</Trigger>
</Style.Triggers>
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
<EventSetter Event="LostFocus" Handler="DataGridCell_OnCellLostFocus" />
</Style>
</DataGrid.Resources>
Here is my c# code:
private void DataGridCell_OnCellLostFocus(object sender, RoutedEventArgs e)
{
System.Windows.Controls.DataGridCell dtgCell = (System.Windows.Controls.DataGridCell)sender;
if (dtgCell.Column.Header.ToString() == "Size")
{
System.Windows.Controls.TextBox tbNewSize = (System.Windows.Controls.TextBox)dtgCell.Content;
Int32 intNewSize = Convert.ToInt32(tbNewSize.Text);
Int32 intCurrSize = Convert.ToInt32(strFieldInfoOrig[dtGrid.Items.IndexOf(dtGrid.CurrentItem), 1]);
if (intNewSize < intCurrSize)
{
string strMsg;
strMsg = "New size, " + intNewSize.ToString() + " is smaller then the original size, " + intCurrSize.ToString();
strMsg += Environment.NewLine;
strMsg += "Due to potential data loss, this is not allowed.";
System.Windows.MessageBox.Show(strMsg);
//dtgCell.Content = intCurrSize.ToString();
dtgCell.Focus();
}
}
}
This is happening because
DataGridTextColumnshows aTextBlockin normal mode andTextBoxwhile editing. So, when this cell loses focus,DataGridTextColumnreturns in normal mode, so its content would be aTextBlockand notTextBoxand hence it shows exception.So, try to cast into
TextBlockand notTextBox.