Hi I have a datagrid and the DataGridTextColumn shown in code below:
<DataGridTextColumn Header="" Width="1*" Binding="{Binding FORECAST_MIN, UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}" >
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}">
<EventSetter Event="LostFocus" Handler="fMin_LostFocus" />
</Style>
</DataGridTextColumn.EditingElementStyle>
Now in the LostFocus event, I'd like to get the parent datagrid from the sender. Code
private void fMin_LostFocus(object sender, RoutedEventArgs e)
{
//Get the datagrid parent
}
Is there a easy way to do so? Thank you. Something like adding a Tag?
#Both Jeff and OptimusPrime's answers work. It only allows me to choose one answer.
Jeff's answer should work. Since you mentioned "Tag". This might be another way to go? Probably not the most elegant way though.
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding FORECAST_MIN, UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}" Tag="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}" LostFocus="fMin_LostFocus"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
and in your code:
private void fMin_LostFocus(object sender, RoutedEventArgs e)
{
var tb = (TextBox)sender;
DataGrid parentDataGrid = (DataGrid)tb.Tag;
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加