简体   繁体   中英

Binding Control DataContext to DataType of DataTemplate?

I have a DataTemplate . I want to bind the DataContext of my control to the DataType of the DataTemplate :

<DataTemplate DataType="{x:Type kitchens:KitchenRule}">
    <controls:KitchenChartView
        DataContext="{Binding}"/>

However, in the constructor of my control code behind, the DataContext is always null?

KitchenRule kitchenRule;

    public KitchenChartView()
    {
        if (this.DataContext is KitchenRule)
        {
            this.kitchenRule = DataContext as KitchenRule;
        }
    }

You can set your DataContext at design time, to avoid it being null:

<UserControl
...
d:DataContext="{d:DesignInstance Type=kitchens:KitchenRule, 
IsDesignTimeCreatable=True}"/>

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