WPF-属性路径可配置的自定义绑定 [英] WPF - Custom Binding where Property Path is configurable

查看:63
本文介绍了WPF-属性路径可配置的自定义绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我告诉你我的情况:
我有一个具有20个依赖项属性的UserControl.我们称它们为DP1,DP2,...,DP20.
然后,在我的屏幕上,我需要使用其中的50个控件.每个DP都绑定到具有相似名称的不同路径:区别在于后缀.对于第一个UC:

< my:UC x:Name = " 
   DP1 = " 
   DP2 = " 
   DP3 = " 
   DP4 = " 
   ...
   DP20 = " 
/>
< my:UC x:Name = " 
   DP1 = " 
   DP2 = " 
   DP3 = " 
   DP4 = " 
   ...
   DP20 = " 
/> 



实际上,UC包含绑定到DP1,DP2等的TextBlock.

如您所见,写入50个UC非常容易出错,每个UC在每个DP中具有相似的属性.

我正在寻找一种在UC中创建称为PostFix的DP的方法.该值将设置为Z01,Z02,...,Z50之类的值.然后,我将为UC中的每个DP扩展Binding(或使用标记扩展),以重建路径.如果可以将UC中的路径设置为Property01_XXX,那很好,但是扩展名可以替换第一个UC中Propert01_Z01的路径,第二个UC中Propert01_Z02的路径.

最好写这样的东西:

< my:UC x:Name = " 
   PostFix = " 
/>
< my:UC x:Name = " 
   PostFix = " 
/> 



关于解决此问题的更好方法的任何想法吗?

现在,我猜测最好的方法是在PostFix setter中为C#中的DP创建所有绑定,并为属性路径创建字符串.

解决方案

您唯一的希望是,然后我将使用DeBug.WriteLine并为生成所有值的循环做一个arbetrry.像这样:

 对于,i  as  整数 =  0    50 
Debug.WriteLine(" & i&  " & i& "  1_Z01}'")
下一个 



那至少比手动做起来容易:) .然后将列表框绑定到集合.不知道这对您的解决方案是否合理.


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:

<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:

<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.

解决方案

If thats your only hope, then I would juse DeBug.WriteLine and make an arbetrry for loop that generates all the values. Like this:

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 :)


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.


这篇关于WPF-属性路径可配置的自定义绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆