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
235
DataContext null when the controls are initiated?
posted

Hi again,

During my evaluation I have encountered a very strange bahavior. I will try to explain it using a very simple test solution. It seems that, when hosting controls using the xamDockManager and setting some of their properties, the DataContext at that point is null (even if the main window's DataContext has been set, and not null). If I am creating the same control outside of the xamDockManager and setting the same property, when setting the property the DataCOntext is not null.

For example I am using a custom control (I just need a custom property, it could be any control):

public class MyCustomControl : Button
    {
        public string SomeTestProperty
        {
            get { return string.Empty; }
            set
            {
                if (DataContext != null)
                    Content = DataContext;
                else Content = "DataContext is null!";
            }
        }
    }

In my main window's constructor I am setting the DataContext to a non null value:

public partial class Window1 : Window
    {
        public Window1()
        {
            DataContext = "DataContext is NOT null.";
            InitializeComponent();
        }
    }

In the XAML code I am creating 2 identical "MyCustomControl" controls, one hosted in the xamDockManager and another outside of it:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
       
        <local:MyCustomControl Grid.Row="0" SomeTestProperty ="something" />
       
        <igDock:XamDockManager Grid.Row="1" Theme="Generic">
            <igDock:XamDockManager.Panes>
                <igDock:SplitPane igDock:XamDockManager.InitialLocation="DockedTop">
                    <igDock:ContentPane x:Name="leftEdgeDock1" Header="Left Edge Dock Pane">
                        <local:MyCustomControl  SomeTestProperty ="something else"/>
                    </igDock:ContentPane>                   
                </igDock:SplitPane>
            </igDock:XamDockManager.Panes>
        </igDock:XamDockManager>
    </Grid>

If I am checking the DataContext again when the Window_Loaded event is being raised I see that the DataContext is not null also for the control hosted in the dock manager. At that point though it is a bit late for us. We could do some work around that but it would be nice if that could be avoided.

Why is this happening? Is there a way to avoid it?

Thank you,

Cristian

 

PlantCML

Parents
No Data
Reply
  • 54937
    Verified Answer
    Offline posted

    The SplitPanes are not part of the logical/visual tree until the xamDockManager has been initialized and therefore they do not get the inherited DataContext until that point. The only way to avoid this would be to explicitly set or bind the DataContext on the ContentPane.

Children