简体   繁体   中英

Binding to a property outside DataGrid

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?

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.

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