Ho un controllo DataGrid da me personalizzato ed avrei bisogno di un aiuto.
La definizione del custom control è questa:
Public Class DataGridExtended
Inherits DataGrid
Sub New()
Dim dict = New ResourceDictionary()
dict.Source = New Uri("DataGridExtendedStyle.xaml", UriKind.RelativeOrAbsolute)
Me.Resources.MergedDictionaries.Add(dict)
Me.SetValue(DataGrid.StyleProperty, Me.FindResource("datagridExtendedStyle"))
End Sub
End Class
lo stile del controllo ridefinisce il template della datagrid ed è così definito (prevede una textbox sopra la datagrid):
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
<Style
x:Key="datagridExtendedStyle"
TargetType="{x:Type DataGrid}">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="DataGrid">
<DockPanel
LastChildFill="True">
<!-- TextBox -->
<TextBox
DockPanel.Dock="Top"
Text="When I click here changes edited on datagrid row are not updated on entity properties, Why? It only works when I change selected row on datagrid."
TextWrapping="Wrap"
Height="40" />
<!-- DataGrid -->
<ScrollViewer
Name="DG_ScrollViewer"
Focusable="false">
<ScrollViewer.Template>
<ControlTemplate
TargetType="{x:Type ScrollViewer}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Auto" />
<ColumnDefinition
Width="*" />
<ColumnDefinition
Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="Auto" />
<RowDefinition
Height="*" />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<Button
Command="{x:Static DataGrid.SelectAllCommand}"
Focusable="false"
Style="{DynamicResource {ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}}"
Visibility="{Binding Path=HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.All}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
Width="{Binding Path=CellsPanelHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<DataGridColumnHeadersPresenter
Name="PART_ColumnHeadersPresenter"
Grid.Column="1"
Visibility="{Binding Path=HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Column}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<ScrollContentPresenter
Name="PART_ScrollContentPresenter"
CanContentScroll="{TemplateBinding CanContentScroll}"
Grid.ColumnSpan="2"
Grid.Row="1" />
<ScrollBar
Name="PART_VerticalScrollBar"
Grid.Column="2"
Maximum="{TemplateBinding ScrollableHeight}"
Orientation="Vertical"
Grid.Row="1"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportHeight}" />
<Grid
Grid.Column="1"
Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="{Binding Path=NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
<ColumnDefinition
Width="*" />
</Grid.ColumnDefinitions>
<ScrollBar
Name="PART_HorizontalScrollBar"
Grid.Column="1"
Maximum="{TemplateBinding ScrollableWidth}"
Orientation="Horizontal"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
ViewportSize="{TemplateBinding ViewportWidth}" />
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter
Name="PART_Items"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ScrollViewer>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Ora ho questo problema...
Quando clicco sulla textbox posta sopra la datagrid, le modifiche editate sulla riga selezionata non vengono "committate" e non vengono aggiornate le proprietà dell'entità collegata alla riga. Coma mai?
Io vorrei evitare questo comportamento e vorrei che la modifiche venissero "confermate" come avviene quando clicco su eventuali textbox esterne al controllo. Come posso farlo?
Per rendere più comprensibile la problematica ho creato un progetto di prova per simulare il problema, che potete scaricare qui:
https://www.dropbox.com/s/e4qr4ook5yr1id7/WpfApplication9b.zip?dl=0Vi ringrazio per ogni informazione/parere utile a riguardo.