WPF custom Control cannot find DataContext

Source: Internet
Author: User
Tags bind bool visibility

The problem is this:

I have customized a control that can be reversed in two states, with the following resource file:

<resourcedictionary xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http ://schemas.microsoft.com/winfx/2006/xaml "xmlns:local=" Clr-namespace:musicmian "&G
    T <style targettype= "{x:type Local:nativecontainer}" > <setter property= "istabstop" Value= "False" ></Se tter> <setter property= "Focusable" value= "False" ></Setter> <setter property= "Template" &G
            T
                    <Setter.Value> <controltemplate targettype= "{x:type Local:nativecontainer}" > <Grid> <VisualStateManager.VisualStateGroups> <visua
                                    Lstategroup name= "Viewstates" > <visualstate name= "Contentview" > <Storyboard> <doubleanimation storyboard.targetname= "T OoLtipborder "storyboard.targetproperty=" Opacity "duration=" 0 "to= 
                                "0"/> </Storyboard> </VisualState> <visualstate x:name= "Tootipview" &
                                    Gt
                                                         <Storyboard> <doubleanimation storyboard.targetname= "Contentborder"
                                                         Storyboard.targetproperty= "Opacity" duration= "0" to= "0"
                                /> </Storyboard> </VisualState> </VisualStateGroup> </vis
        ualstatemanager.visualstategroups> <border name= "Contentborder" >                    <contentpresenter verticalalignment= "Stretch" DataCo ntext= "{TemplateBinding DataContext}" horizontalalignment= "Stretch" content=
                        "{TemplateBinding Contentview}" ></ContentPresenter> </Border>
                                              <border name= "Tooltipborder" > <contentpresenter verticalalignment= "Stretch"
                                              datacontext= "{TemplateBinding DataContext}"
                        Horizontalalignment= "Stretch" content= "{TemplateBinding Tooltipview}" ></ContentPresenter> </Border> </Grid> </ControlTemplate> </sette r.value> </Setter> </Style> </ResourceDictionary>
The function code is as follows:
public class Nativecontainer:control {static Nativecontainer () {Defaultstylekeypropert
            Y.overridemetadata (typeof (Nativecontainer), New Frameworkpropertymetadata (typeof (Nativecontainer))); Isfilpedproperty = Dependencyproperty.register ("isfilped", typeof (BOOL), typeof (Nativecontainer), new
            Frameworkpropertymetadata (false,new propertychangedcallback (isfilpedchanged)));
            Contentviewproperty = Dependencyproperty.register ("Contentview", typeof (Object), typeof (Nativecontainer));
        Tooltipviewproperty = Dependencyproperty.register ("Tooltipview", typeof (Object), typeof (Nativecontainer));
            } private static void Isfilpedchanged (DependencyObject D, DependencyPropertyChangedEventArgs e) {
            Nativecontainer NC = (nativecontainer) D; NC.
        Changedvisualstate (FALSE); } private void Changedvisualstate (bool p) {if (isfilped) {Visual StatEmanager.gotostate (This, "Contentview", p);
            } else {visualstatemanager.gotostate (this, "Tootipview", p);
            } UIElement CView = Contentview as UIElement; if (CView! = null) {if (isfilped) {cview.visibility = Vi Sibility.
                Visible;
                } else {cview.visibility = Visibility.hidden;
            }} UIElement TView = Tooltipview as UIElement; if (TView! = null) {if (isfilped) {tview.visibility = Vi Sibility.
                Hidden;
                } else {tview.visibility = visibility.visible;
        }}} public static readonly DependencyProperty Isfilpedproperty; public static readonly DependeNcyproperty Contentviewproperty;
        public static readonly DependencyProperty Tooltipviewproperty;
            public bool Isfilped {get {return (bool) GetValue (Isfilpedproperty);}
        set {SetValue (Isfilpedproperty, value);}
            Public object Contentview {get {return (object) GetValue (Contentviewproperty);}
        set {SetValue (Contentviewproperty, value);}
            Public object Tooltipview {get {return (object) GetValue (Tooltipviewproperty);}
        set {SetValue (Tooltipviewproperty, value);} } public override void OnApplyTemplate () {base.
            OnApplyTemplate ();
        Changedvisualstate (FALSE); }
    }
The code is simple and does not explain.

The problem I'm having now is that I put two of these custom controls into the two tabitem of TabControl, the same data source binding method (Contentview in the custom control), the first one to bind to the data, and the second one to bind to the data. Which hero can help me solve this problem, express my thanks ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.