开发者

Binding a dependency property to another dependency property

Take this scenario where I am working with a grid like control:

<RadGrid DataContext={Binding someDataContextObject, 开发者_运维知识库Mode=OneWay}>
   <RadGrid.columns>
      <RadGrid.Column Header="Column Header"
                            DataMember="{Binding dataContextObjectProperty, Mode=OneWay}">
           [...]
           <DataTemplate>
                <MyCustomControl Data="{Binding ???}" />
           </DataTemplate>
      <\RadGrid.Column>
   </RadGrid.columns>   
</RadGrid>

I would like to bind the Data dependency property of MyCustomControl to the DataMember dependency property of the column to avoid multiple bindings to the same data. How do I do it?


It seems that there is not easy way to accomplish that task. The problem is that it is not possible to bind something inside of DataTemplate to something that is outside the data template. You could find some kind of workaround here.

However I don't think this deserves that kind of effort. What is the problem having multiple bindings to the same data?

As they say If something is so difficult may be you doing it the wrong way. Do you really needs to bind your custom control to the whole data context? Basically when you give the grid the data source it will take care to pick a single row from your data context and set it as DataContext for your template. However if you have a good reason doing it that way you should check out the link from the first paragraph.

Hope this helps!


Here are a few ideas to give you a start. I haven't tested these so don't be surprised if they do not work.

  1. Give your MyCustomControl a name and then use the ElementName property in your binding to refer to it.
  2. Use RelativeSource binding and set the ancestor type to the type of your MyCustomControl class.
  3. Bind to a property of your RadGrid such as the tag and then attempt to bind in your data template to pull from the RadGrid's Tag property. (Warning code-smell!)

Hope you find a solution that works for you, and please don't forget to come back and post your answer to help out the rest of us programmers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜