I have the following XAML code as part of a custom control:
<telerik:RadTreeView x:Name="treeModules">
<telerik:RadTreeView.ItemTemplate>
<core:HierarchicalDataTemplate ItemsSource="{Binding Submodules}">
<local:ModuleInfoUserControl IsReadOnly="{Binding ElementName=ctrlIsReadOnly, Path=IsReadOnly}"/>
</core:HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>
</telerik:RadTreeView>
Where:
- The
ItemsSource
property oftreeModules
is set by code to a list ofModule
objects. TheModule
class has a property namedSubmodules
, which consists on a collection of more Modules. ModuleInfoUserControl
is a custom control used to display module information.IsReadOnly
is a dependency property declared on the control.ctrlIsReadOnly
is a hidden TextBox defined elsewhere in the main user control. It is used to make a bunch of controls editable or read only.
The problem: binding to ctrlIsReadOnly
element in ModuleInfoUserControl
does not work. The IsReadOnly
property of it is always at its default state, even if the corresponding property of crlIsReadOnly
changes.
But, if I change the RadTreeView into a standard Silverlight TreeView, and the HierarchicalDataTemplate into the standard Silverlight version too, it works as expected.
Ok, up to here I have pasted an exact copy of the support ticket I have sent to Telerik in the hope that they could help me. I must say that the reply has been quick and as efficient as it could be: they have said me that they could not reproduce the bug, and have send me a small test project that exercises the offending piece of code. The test project shows the bug on my machine, but works on Telerik's.
An开发者_C百科d here comes the really strange part. I have published Telerik's sample project on my personal site. Here is the link:
http://www.konamiman.com/TelerikTest/TestPage.html
The idea is that when pressing the "ToggleIsReadOnly" button, the "Root" TextBox should become read-only. But again, it works on Telerik's machines, but not on mine! So the application code itself is apparenly ok.
Thinking that it could be a problem on my machine's Silverlight runtime, I have tested a bunch of others, including: Spanish Windows 7 32 and 64 bits, Spanish Windows XP, English Windows Server 2008, and even on my wife's computer with Japanese Windows XP. On all of these, it does NOT work as expected.
So, what's happening here? I am completely stuck.
By the way, Silverlight Runtime I'm using is version 3.0.50106.0.
UPDATE: I have uploaded the source code of the test project as well. Here is the URL:
http://www.konamiman.com/TelerikTest/TreeViewTest.zip
I have tried the test project now, and I can not get it to work. I have tried changing the thread culture to "en-US", but that does not help. It would appear that items controls a HierarchicalDataTemplate cannot bind to properties on elements outside of that datatemplate. I think you should get back to Telerik and ask them again if they are 100% sure that this works as expected on their machines. If it does, it is a very strange bug.
[EDIT]
I have found some more info, see the links below. It looks like this is a general silverlight problem. But it is still strange that the Telerik guy claims that it works on his machine.
http://forums.silverlight.net/forums/p/108804/267789.aspx
And here is a similar thing but with the Telerik GridView component:
http://www.telerik.com/community/forums/silverlight/gridview/elementname-binding-inside-of-celltemplate.aspx
I have found an alternative approach that seems to work fine. Instead of defining ctrlIsReadOnly
as part of the main user control tree, I define it as a static resource:
<UserControl.Resources>
<TextBox x:Key="ctrlIsReadOnly" IsEnabled="True" IsReadOnly="False" Visibility="Collapsed" />
</UserControl.Resources>
Then I just have to slightly change the binding declaration on ModuleInfoUserControl:
<local:ModuleInfoUserControl IsReadOnly="{Binding Source={StaticResource ctrlIsReadOnly}, Path=IsReadOnly}"/>
I then access ctrlIsReadOnly from code this way: ((TextBox)this.Resources["ctrlIsReadOnly"])
精彩评论