I have a button, when i use datacontext and command, the command not work:
<Button DataContext="{Binding horaires}" Style="{StaticResource RefreshAppBarButtonStyle}" AutomationProperties.Name="{Binding HomeAppBarTitle, Converter={StaticResource StringResourceConverter}}" Command="{Binding RefreshCommand}" ></Button>
when i delete the datacontext and set the name manually it’s work:
<Button Style="{StaticResource RefreshAppBarButtonStyle}" AutomationProperties.Name="Actualiser" Command="{Binding RefreshCommand}" ></Button>
what’s can be the problem??
Best regards
Does
horaireshave a property calledRefreshCommand? I’m guessing not because you said it works without theDataContextbeing set.You need to either remove the
DataContextbinding and change theAutomationProperties.Nameproperty binding to use thehorairesprefix like this:Or use a
RelativeSourceorElementNamebinding to find the UI element that has yourRefreshCommandin itsDataContext. For example,Personally, I would go with the first one because I like to avoid setting the
DataContextexplicitly if possible. The UI is supposed to reflect the data behind it, and setting theDataContextmanually changes this hierarchy.