0

I've got a problem with the WPF DataGrid which I can't really explain/solve.

When I want to type in some values into a cell, I'm only "allowed" to type exactly one character, then the DataGrid cell loses focus and the whole row is selected.

I can "solve" this problem when I remove the databinding from the DataGridTextColumn from code below.

<DataGrid x:Name="dataGrid1" ItemsSource="{Binding Items,UpdateSourceTrigger=PropertyChanged,Mode=OneWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanUserAddRows="True" CanUserDeleteRows="True" AutoGenerateColumns="False" Height="auto" Width="auto" Margin="2,0,0,0">
    <i:Interaction.Triggers>
          <i:EventTrigger EventName="SelectionChanged">
                <cmd:EventToCommand Command="{Binding  SelectionChangedCommand,Mode=OneWay}" CommandParameter="{Binding SelectedItems}">
                </cmd:EventToCommand>
           </i:EventTrigger>
     </i:Interaction.Triggers>
      <DataGrid.Columns>
      <DataGridTextColumn Header="SampleHeader" Binding="{Binding Path=SampleText, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Width="auto"></DataGridTextColumn>
   </DataGrid.Columns>
</DataGrid>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
nukleos
  • 419
  • 2
  • 8
  • 19

1 Answers1

1

It will help if you precise the expected behavior.

I mean ok you would like to type some characters, and then fire an event when your text column lost focus? If so, what is the purpose of UpdateSourceTrigger=PropertyChanged in the DataGridTextColumn?

As explain here, you could fire an event each time you type a character, or on focus lost (which is the default behavior): Fire TextBox.TextChanged immediately when text is typed in

Thus the first question is why you need to raise an event by DataGridTextColumn each time you type a char? I suppose in your case that the code managing this change raise something else (another propertyChanged, selectionChanged, a command or anything) that should explain why your textBox lost focus.

Community
  • 1
  • 1
Ouarzy
  • 3,015
  • 1
  • 16
  • 19
  • Sorry for the late answer. This behavior I currently have with datagrid is really weird.it has nothing to do with commands and/or focus lost event. I only have some Bindings (only to get some data from db) with observablecollection to datagridtextcolumns. The problem is i'm only able to type/change one char in a cell and get "thrown out" of the cell, focus gets lost. – nukleos Jun 07 '13 at 20:35
  • oh, I found the source of evil. I'm using TrulyObservableCollection from this post: http://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop . that caused the problem with grid view. weird. thanks for your time – nukleos Jun 07 '13 at 20:39