0

I'm writing a WPF application and my problem is that I have a class member bound with the property "IsEnabled" of one element of xaml and basically the binding seems not to work at all.

Here some extracts of my code:

XAML

<Window.Resources>
   <Style TargetType="{x:Type DockPanel}" x:Key="MyDockPanel">
      <Style.Triggers>
         <Trigger Property="IsEnabled" Value="False">
           <Setter Property="Background" Value="Gray"/>
          </Trigger>
       </Style.Triggers>
    </Style>
</Window.Resources>


<DockPanel x:Name="home_dock_panel" Style="{StaticResource MyDockPanel}" HorizontalAlignment="Left" Height="35" LastChildFill="False" Margin="22,82,0,0" VerticalAlignment="Top" Width="189" MouseDown="GoToHome" IsEnabled="{Binding Path=UnlockMenu}">
   <Image Source="images/home.png" Height="26" RenderTransformOrigin="1.277,0.518" Margin="0,5,0,4.2"/>
   <Label Content="Home" Height="35" VerticalAlignment="Top"  Foreground="White" FontSize="20" FontWeight="Bold"/>
</DockPanel>

Here the function that allows changing page

private void GoToXPage(object sender, MouseButtonEventArgs e)
{
    if(central_frame.Content.GetType() != typeof(LathePage))
    {
       ViewModel viewModel = new ViewModel();
       central_frame.Content = new XPage(viewModel);
       panel_dock.DataContext = viewModel;

     }

Here a part of ViewModel code

public class ViewModel : INotifyPropertyChanged
{
     public GenerateCommand Generate { get; set; }
     public bool UnlockMenu { get; set; }

     public ViewModel()
     {   
        Generate = new GenerateCommand();
        Generate.PropertyChanged += InnerPropertyChanged;
        UnlockMenu = Generate.UnlockMenu;
     }

    private void InnerPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      if(e.PropertyName == "UnlockMenu")
      {
        UnlockMenu = Generate.UnlockMenu;
        NotifyPropertyChanged("UnlockMenu");
      }
  
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string name)
    {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

In the class GenerateCommand I call NotifyPropertyChanged("UnlockMenu").

Does anyone have some suggestions on this problem?

Francesco
  • 460
  • 1
  • 4
  • 11
  • Where do you set the `DataCOntext` of the `Window`? You must set the `DataContext` of the `Window` to an instance of `ViewModel`, so that the `DockPanel.DataContet` has the right value in order to make the bindings resolve. – BionicCode Sep 13 '20 at 18:36
  • Don't use magic strings. Use `nameof` to generate the string representation of types or type members: `NotifyPropertyChanged(nameof(this.UnlockMenu));` Now when you rename `UnlockMenu` you don't have to change all those strings too. You also eliminate typos. – BionicCode Sep 13 '20 at 18:48
  • Or use `void OnPropertyChanged([CallerMemberName] string prop = null)`, You should have a MainVM that would be the data context of main window and `central_frame` is a `Frame` element then read [this](https://stackoverflow.com/a/3643684/2029607). Make sure this is what you want. All of this can be replaced with `ContentControl` and a fraction of your code. – XAMlMAX Sep 14 '20 at 15:14
  • @Francesco: What's the difference between `panel_dock` and `home_dock_panel`? Did you intend to set the `DataContext` of the latter in your `GoToXPage` method? – mm8 Sep 15 '20 at 14:16

0 Answers0