Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Let me tell you my case:
I've got a UserControl with 20 dependency properties. Let's call them DP1, DP2, ..., DP20.
Then, on my screen I need to use 50 of these controls. Each DP is binded to a different path, with similar names: the difference is the postfix. For the first UCs:
C#
<my:UC x:Name="uc1"
   DP1="{Binding Path=Propert01_Z01}"
   DP2="{Binding Path=Propert02_Z01}"
   DP3="{Binding Path=Propert03_Z01}"
   DP4="{Binding Path=Propert04_Z01}" 
   ...
   DP20="{Binding Path=Propert20_Z01}" 
/>
<my:UC x:Name="uc2"
   DP1="{Binding Path=Propert01_Z02}"
   DP2="{Binding Path=Propert02_Z02}"
   DP3="{Binding Path=Propert03_Z02}"
   DP4="{Binding Path=Propert04_Z02}" 
   ...
   DP20="{Binding Path=Propert20_Z02}" 
/>


Actually, UC contains TextBlocks binded to DP1, DP2, and so on.

As you can see is very error prone to write 50 UCs, each one with a similar property in each DP.

I'm looking for a way to create a DP in UC called PostFix. This will be set to a value like Z01, Z02, ..., Z50. Then I'll extend Binding (or use a markup extension) for each DP in UC, in order to rebuild the path. It would be nice if I can set the path in the UC as Property01_XXX, but the extension can replace the path for Propert01_Z01 in the first UC, Propert01_Z02 for the second.

It would be nice to write something like this:
C#
<my:UC x:Name="uc1"
   PostFix="Z01" 
/>
<my:UC x:Name="uc2"
   PostFix="Z02" 
/>


Any idea about the better way to resolve this?

Right now, I'm guessing the best is to create in PostFix setter all the bindings for DPs in C#, creating the string for Property Paths.
Posted
Updated 31-Jul-12 8:34am
v2
Comments
Kenneth Haugland 31-Jul-12 14:30pm    
Should be really tempting to use a OBservableCollection on this instead of all the writing in XAML.

If thats your only hope, then I would juse DeBug.WriteLine and make an arbetrry for loop that generates all the values. Like this:
VB
For i as integer = 0 to 50
Debug.WriteLine("DP" &i &"='{Binding Path=Propert0" &i &"1_Z01}'")
next


Thats at lest easyer than manually do it :)
 
Share this answer
 
Another option is to use a listbox control and have the ViewModel contain an IEnumerable collection of the class that includes the information you need to be the itemssource. Then bind the listbox to the collection. Don't know if this is reasonable for your solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900