Trigger like solution for DataGridCell using DataTriggerBehavior (Avalonia.Xaml.Interactivity) #8121
-
Hello! Please tell me how I can implement trigger for each DataGrid cell related to Age property throw ChangePropertyAction ? I have not yet been able to implement this through table styles. Is this the right decision? Thanks!
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
I think you can attach a behavior to the control using styles. @wieslawsoltes does it work with stable branch and is there any example of that? So, in your case you would need to set CellClasses on the right column (let's say "ageColumn"). |
Beta Was this translation helpful? Give feedback.
-
Thanks for the useful hints! As I am looking for the same answer I have reconstructed the code that is close to your hints: <Window.Styles>
<Style Selector="DataGridCell.statusColumn">
<Setter Property="FontSize" Value="24"/>
<Setter Property="(int:Interaction.Behaviors)">
<int:BehaviorCollectionTemplate>
<int:BehaviorCollection>
<ia:DataTriggerBehavior Binding="{Binding Status}" ComparisonCondition="Equal" Value="Rejected">
<ia:ChangePropertyAction TargetObject="DataGridCell" PropertyName="Background" Value="Yellow" />
</ia:DataTriggerBehavior>
</int:BehaviorCollection>
</int:BehaviorCollectionTemplate>
</Setter>
</Style>
</Window.Styles>
...
<DataGrid AutoGenerateColumns="False" Margin="10"
Items="{Binding Banknotes}" SelectedItem="{Binding SelectedBanknote}">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static p:Resources.NoteId}" Binding="{Binding Id}" />
<DataGridTextColumn Header="{x:Static p:Resources.NoteCurrency}" Binding="{Binding Currency}" />
<DataGridTextColumn Header="{x:Static p:Resources.NoteDenomination}" Binding="{Binding Denomination}" />
<DataGridTextColumn Header="{x:Static p:Resources.Status}" Binding="{Binding Status}" CellStyleClasses="statusColumn" />
</DataGrid.Columns>
</DataGrid> Let me explain my intentions: I want to attach the behaviour for replacing the Background property to every DataGridCell object that has a "statusColumn" class. I use the BehaviorCollection because it attaches a behaviour to each object according to the selector. The code almost works. The only problem I have is that the binding doesn't use the DataGridCell's DataContext (the element of the ObservableCollection from the ViewModel), but it uses the DataContext of the whole View/Window (ViewModel). Is there any way to specify that I need the DataGridCell's DataContext and not the View's DataContext? |
Beta Was this translation helpful? Give feedback.
-
Accepted answer in this comment and following, for anyone interested: #8121 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Accepted answer in this comment and following, for anyone interested: #8121 (reply in thread)