Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
415
Proper way to set the datacontext of a ContentPane?
posted

I need to bind a property in my viewmodel to a property in a contentpane (IsEnabled for example).   The following doesnt work, as I get an error that SetIsEnabledInTheVM is not a dependency property:

<igDock:TabGroupPane>

  <igDock:ContentPane x:Name="contentPane">

      <ContentControl>

          <vm:MyViewModel SetIsEnabledInTheVM="{Binding ElementName=contentPane, Path=IsEnabled}" />

      </ContentControl>

  </igDock:ContentPane>

</igDock:TabGroupPane> 

After some reading up, I think this should work if the VM is set as the datacontext of the ContentPane (or ContentControl?).  But even if I do set the datacontext to my vm, I still dont see my view properly.  Where should I be setting the datacontext here, and how do I then access the SetIsEnabledInTheVM property?

Thanks!

Parents
  • 54937
    Offline posted

    nicros said:
    I need to bind a property in my viewmodel to a property in a contentpane (IsEnabled for example).   The following doesnt work, as I get an error that SetIsEnabledInTheVM is not a dependency property:

    You can only set a Binding on a DependencyProperty. Essentially what you have written would essentially be like saying that the SetIsEnabledInTheVM property (assuming it were a DP) should have its value set to the value of the IsEnabled property of the ContentPane. Even if that's what you wanted, this wouldn't work anyway because ViewModel is not in the visual tree and doesn't have an inheritance context so it has no access to the elements in the xaml. I think what you meant to do was have the IsEnabled property of the ContentPane be based upon that property in your VM. Note, I'm assuming that you have a DataTemplate defined for your VM type.

       <igDock:TabGroupPane>
          <igDock:ContentPane 
            IsEnabled="{Binding SetIsEnabledInTheVM}"
            x:Name="cp1">
              <igDock:ContentPane.DataContext>
                  <vm:MyViewModel />
              </igDock:ContentPane.DataContext>
          </igDock:ContentPane>
        </igDock:TabGroupPane>

    Or if you really want the VM to be hosted within a ContentControl that is within the ContentPane:

       <igDock:TabGroupPane>
          <igDock:ContentPane 
            DataContext="{Binding Path=Content, ElementName=cp1Content}" 
            IsEnabled="{Binding SetIsEnabledInTheVM}"
            x:Name="cp1">
              <ContentControl x:Name="cp1Content">
                  <vm:MyViewModel />
              </ContentControl>
          </igDock:ContentPane>
        </igDock:TabGroupPane> 
Reply Children