Re: Specify ResourceDictionary and ResourceKey in Binding
Posted: 18 Aug 2015, 10:56
Hi,
I have investigated further that issue.
I think the problem has nothing to do with the C++ API.
I've done a minimal project to reprodute the issue. Here is my code:
TestControl.xaml
I also have a RootModel, which is bound to the application root (ROOT) in C++, containing a ResourceDictionary of String (called Bank). This Bank should be bound to the TestConverter in TestControl (TestConverter turns its argument into a key to interrogate the Bank and returns the associated value). The Converter is finally used for the Text property in the TextBlock in TestControl. Finally, TestControl uses its Data Context to get the key to interrogate the Bank.
This does not work, because it seems TestConverter cannot find the Bank field in the root data context. However, if I don't set any Data Context for TestControl (and uses the Tag property to write the key for instance), it works juste fine.
My guess is that the Converter interrogate the first Data Context it finds and does not go upper in the element tree. Is that an expected behaviour?
I have investigated further that issue.
I think the problem has nothing to do with the C++ API.
I've done a minimal project to reprodute the issue. Here is my code:
Code: Select all
<Border
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="ROOT">
<Border.Resources>
<KeyModel x:Name="Model" Key="SomeKey"/>
</Border.Resources>
<StackPanel
Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Border x:Name="PART_Slot1">
<TestControl DataContext="{StaticResource Model}"/>
</Border>
<Border x:Name="PART_Slot2"/>
</StackPanel>
</Border>
Code: Select all
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="TestControl" x:Name="ROOT_TestControl">
<UserControl.Resources>
<TestConverter x:Name="Converter" Test="{Binding Bank}"/>
</UserControl.Resources>
<Border
BorderThickness="1"
BorderBrush="Red">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Text: "/>
<TextBlock
Text="{Binding Key, Converter={StaticResource Converter}}"/>
</StackPanel>
</Border>
</UserControl>This does not work, because it seems TestConverter cannot find the Bank field in the root data context. However, if I don't set any Data Context for TestControl (and uses the Tag property to write the key for instance), it works juste fine.
My guess is that the Converter interrogate the first Data Context it finds and does not go upper in the element tree. Is that an expected behaviour?