简体   繁体   中英

WPF DataGridCell Style Trigger based on IsSelected only works once

I have been trying to make single edit click for a WPF DataGrid and I have looked at a lot of solutions on stackoverflow but have an additional use case that has not been addressed. I need to setup a style trigger but I need it to not just set IsEditing to true all the time. I need it based off of a second condition. The problem I'm running into is that it only works once per cell.

Here is the XAML that almost works.

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
   <Style.Triggers>
      <MultiDataTrigger>
         <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
            <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.IsEditing}" Value="True" />
         </MultiDataTrigger.Conditions>
         <Setter Property="IsEditing"  Value="true" />
      </MultiDataTrigger>
   </Style.Triggers>
</Style>

The problem with this is that it only works once per cell. Now if I change the first condition to be based on IsFocused it works all the time. The problem with that is then I can't click in the textbox within the cell for obvious reasons. I am really at a loss for why IsSelected only works once per cell. By the way if I take the view out of edit mode and put it back in using a button that is also on the view it will work again...once for each cell.

Does anyone have any insight into why this odd behavior happens?

Ok. I do not understand why IsSelected doesn't work but I did find a property that does work AND let's me type into the textbox in the cell. The trick was to use IsKeyboardFocusWithin property. Here is the complete solution that works as expected:

<DataGrid.CellStyle>
   <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
      <Style.Triggers>
         <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
               <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsKeyboardFocusWithin}" Value="True" />
               <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.IsEditing}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter Property="IsEditing"  Value="true" />
         </MultiDataTrigger>
      </Style.Triggers>
   </Style>
</DataGrid.CellStyle>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM