0

I have DataGrid with multiple DataGridTemplateColumn. In one of these columns, I have a button that I want to show when the user select the row and also If CodeFonc is equal to 1. (CodeFonc is not part of DataGrid/SelectedItem)

ViewModel:

   private int _codeFonc;
    public int CodeFonc
    {
        get { return _codeFonc; }

        set
        {
            _codeFonc = value;
            NotifyOfPropertyChange("CodeFonc");
        }
    }

XAML:

 <MultiDataTrigger>
   <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}" Value="True"/>
        <Condition Binding="{Binding Path=CodeFonc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="1" />
    </MultiDataTrigger.Conditions>
   <Setter Property="Button.Visibility" Value="Visible" />
     </MultiDataTrigger>

I'm getting CodeFonc from another window but I'm 100% sure that its value is 1 because I confirmed while debugging.

Most importantly: If I ever replace the CodeFonc with anything from the DataGrid (SelectedItem) itself, the button will show fine. So my guess is that because the XAML above is inside the DataGrid, its limiting it somehow?

Hackawar
  • 25
  • 1
  • 6
  • 1
    You need to bind CodeFonc a little different because inside the DataGrid there is no direct access to "global" DataContext-only to local item. Please look into [These-Answer](https://stackoverflow.com/questions/3920040/binding-to-datacontext-outside-current-itemssource-context) If following these link you are not able to do these let me know I will write a full answer. – Kaspar Jul 26 '18 at 09:08
  • @Kaspar worked! thank you. – Hackawar Jul 26 '18 at 09:18
  • Have you set the `DataContext` in the xaml? – Alfie Jul 26 '18 at 16:27

1 Answers1

0

If the DataContext has not been set in the xaml, you can set it to the ViewModel like so:

<Window.DataContext>
    <local:ViewModel/>
</Window.DataContext>

Where ViewModel is the name of the ViewModel.

Alfie
  • 1,903
  • 4
  • 21
  • 32