locked
Global XML Data Provider and Templates (Agave) - 1/3/2008 12:01 PM PST RRS feed

  • Question

  • By: Agave


    I'd like to have my XmlDataProvider and Data Templates defined globally, so 
    they can be defined in one place, but accessible for data binding by all XAML 
    and .CS code-behind files.
    
    Is this possible and what's the best way to do it?
    -- 
    TIA, Agave
    Friday, February 22, 2008 3:27 PM

Answers

  • Corrado Cavalli [MVP]


    You can define it inside Application Object programmatically

    public partial class App : Application
    {
    ObjectDataProvider odp;


    protected override void OnStartup (StartupEventArgs e)
    {
    base.OnStartup(e);
    odp = new ObjectDataProvider();
    }

    public ObjectDataProvider Data
    {
    get { return odp; }
    }
    }

    then in every Window where you need to get the data you might set a Window
    wide DataContext this way

    public Window1 ()
    {
    InitializeComponent();
    this.DataContext = (App.Current as App).Data;
    }

    The same can be done in XAML but you' have to do the same assignment using
    something like

    this.DataContext = this.FindResource("MyData");

    HTH

    --
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET -
    http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    --
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET -
    http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    Friday, February 22, 2008 11:37 PM

All replies

  • Corrado Cavalli [MVP]


    You can define it inside Application Object programmatically

    public partial class App : Application
    {
    ObjectDataProvider odp;


    protected override void OnStartup (StartupEventArgs e)
    {
    base.OnStartup(e);
    odp = new ObjectDataProvider();
    }

    public ObjectDataProvider Data
    {
    get { return odp; }
    }
    }

    then in every Window where you need to get the data you might set a Window
    wide DataContext this way

    public Window1 ()
    {
    InitializeComponent();
    this.DataContext = (App.Current as App).Data;
    }

    The same can be done in XAML but you' have to do the same assignment using
    something like

    this.DataContext = this.FindResource("MyData");

    HTH

    --
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET -
    http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    --
    Corrado Cavalli [Microsoft .NET MVP-MCP]
    UGIdotNET -
    http://www.ugidotnet.org
    Weblog: http://blogs.ugidotnet.org/corrado/
    Friday, February 22, 2008 11:37 PM
  • Agave


    Thanks Corrado.
    -- 
    Agave
    
    
    "Corrado Cavalli [MVP]" wrote:
    
    > You can define it inside Application Object programmatically
    > 
    > public partial class App : Application
    >   {
    >   ObjectDataProvider odp;
    > 
    > 
    >   protected override void OnStartup (StartupEventArgs e)
    >   {
    >    base.OnStartup(e);
    >    odp = new ObjectDataProvider();
    >   }
    > 
    >   public ObjectDataProvider Data
    >   {
    >    get { return odp; }
    >   }
    >   }
    > 
    > then in every Window where you need to get the data you might set a Window 
    > wide DataContext this way
    > 
    > public Window1 ()
    >   {
    >    InitializeComponent();
    >    this.DataContext = (App.Current as App).Data;
    >   }
    > 
    > The same can be done in XAML but you' have to do the same assignment using 
    > something like
    > 
    > this.DataContext = this.FindResource("MyData");
    > 
    > HTH
    > 
    > -- 
    > Corrado Cavalli [Microsoft .NET MVP-MCP]
    > UGIdotNET - http://www.ugidotnet.org
    > Weblog: http://blogs.ugidotnet.org/corrado/
    > -- 
    > Corrado Cavalli [Microsoft .NET MVP-MCP]
    > UGIdotNET - http://www.ugidotnet.org
    > Weblog: http://blogs.ugidotnet.org/corrado/ 
    > 
    > 
    Friday, February 22, 2008 11:38 PM