![]() |
|
|
#1 |
|
Guest
Posts: n/a
|
Databinding to a singleton
I have a class which only has a private constructor (a singleton), where I
have a public static property named Instance which returns the one and only class. When I attempt to databind, this class does not show up in the "clr" list of classes (I'm guessing because it's got a private constructor). Anybody have any ideas on how I would databind to this class using blend? I'm guessing it would be some sort of custom expression, but have experimented and failed so far. Thanks -- Jim |
|
|
|
#2 |
|
Guest
Posts: n/a
|
Re: Databinding to a singleton
Have you tried adding manually an ObjectDataProvider to Window Resources,
ObjectDataProvider can bind to static methods. -- Corrado Cavalli [Microsoft .NET MVP-MCP] UGIdotNET - http://www.ugidotnet.org Weblog: http://blogs.ugidotnet.org/corrado/ |
|
|
|
#3 |
|
Guest
Posts: n/a
|
Re: Databinding to a singleton
Wow Corrado, pointing me to ObjectDataProvider sure made for a busy day of
research and test coding... this is weighty stuff. I've almost got this working, but just a bit short on a "complete solution". Here's what I have so far... 1. I have a singleton derived from ObservableCollection, and it has a method named "GetInstance" which will return the one-and-only instance. 2. In my App.xaml file, I have manually added 2 lines: a) xmlns:Sample="clr-namespace BSample"b) In the Application.Resources have added: <ObjectDataProvider MethodName="GetInstance" ObjectType="{x:Type Sample:ContactCollection}" x:Key="ContactCollectionSingleton"/> I have built a window to display a listbox where I have populated the ContactCollection with sample data. This window consists of the layoutroot, a child grid, and a listbox in that grid. Following is the steps in Blend I used to make this work: 1. When setting the grid's DataContext, I found that the proper context can be set from the "Local Resources" selection, where "ContactCollectionSingleton" was present. (While this is different from the normal "Select DataBinding, and bind to a clr object" step, this is something I feel I can sell to our designers.) 2. When setting the listbox's "ItemSource", the only way I could get this to work was to select "Custom Expression" and type in the following: {Binding Mode=OneWay, Source={StaticResource ContactCollectionSingleton}} This actually works, and the listbox displays the data as it should when run, but the step of typing in a custom expression is far from designer friendly. Can you think of anything I can do to be able to present this data to Blend in such a way that there is something to select under the normal "Create Data Binding" window you get when you select "Data Binding"? I don't think it's too much of a problem to manually add the resource I added into the App.xaml file... this just seems like more of a workflow issue, where when I create a new singleton that is bindable, I also edit App.xaml to expose it to blend... no problem. But having the "non-programmer" designers (who can handle quite a bit) type in that custom expression is not going to make me any friends, not to mention its very prone to error. Thanks for the help so far, it was an interesting day of study so far. -- Jim "Corrado Cavalli [MVP]" wrote: > Have you tried adding manually an ObjectDataProvider to Window Resources, > ObjectDataProvider can bind to static methods. > > -- > Corrado Cavalli [Microsoft .NET MVP-MCP] > UGIdotNET - http://www.ugidotnet.org > Weblog: http://blogs.ugidotnet.org/corrado/ > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Re: Databinding to a singleton
This is one of the areas where Blend might improve...
Regarding binding: If you associate data to Grid's DataContext and listbox is inside the grid you can just use (via Custom Expression) ItemsSource={Binding}. Regarding design time data: You have to modify your class so that when instantiated at design time by Blend (that's what really happens under the hood) it provides sono dummy data. You can find a detailed sample here: http://www.galasoft.ch/mydotnet/arti...007091401.html -- Corrado Cavalli [Microsoft .NET MVP-MCP] UGIdotNET - http://www.ugidotnet.org Weblog: http://blogs.ugidotnet.org/corrado/ |
|
|
|
#5 |
|
Guest
Posts: n/a
|
Re: Databinding to a singleton
Thanks Corrado, that did the job! And thank you for the added link for
design time data... that was a great addition to your response. This is 100% inline with my desire to provide the best design time experience I am able to. -- Jim "Corrado Cavalli [MVP]" wrote: > This is one of the areas where Blend might improve... > Regarding binding: If you associate data to Grid's DataContext and listbox > is inside the grid you can just use (via Custom Expression) > ItemsSource={Binding}. > Regarding design time data: You have to modify your class so that when > instantiated at design time by Blend (that's what really happens under the > hood) it provides sono dummy data. > You can find a detailed sample here: > http://www.galasoft.ch/mydotnet/arti...007091401.html > > -- > Corrado Cavalli [Microsoft .NET MVP-MCP] > UGIdotNET - http://www.ugidotnet.org > Weblog: http://blogs.ugidotnet.org/corrado/ > > |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|