Specify ResourceDictionary and ResourceKey in Binding
Posted: 09 Mar 2015, 18:40
Hi everyone,
In order to have a set of common resources for my application without having several instances of the same ResourceDictionary, I'd like to be able to specify what UI component requires what Dictionary in the code. The thing is I don't want to add them as MergedDictionaries, because I don't want to end up with conflicts due to multiple resources having the same key. To achieve that, I want each ResourceDictionary to be an entry in the Control Resources and then be able to specify which dictionary should be interrogated with what key.
basicDictionary.xaml being:
This code does not get me any error, but nothing is displayed. I've tried to use a converter, explicitly turning the argument into a ResourceKey for the ResourceDictionary:
But the value of strKey is wrong, therefore the key is null and I retrieve nothing from my Dictionary.
What am I doing wrong? Is there a better way to perform what I'm trying to do?
Thanks,
Alexandre.
In order to have a set of common resources for my application without having several instances of the same ResourceDictionary, I'd like to be able to specify what UI component requires what Dictionary in the code. The thing is I don't want to add them as MergedDictionaries, because I don't want to end up with conflicts due to multiple resources having the same key. To achieve that, I want each ResourceDictionary to be an entry in the Control Resources and then be able to specify which dictionary should be interrogated with what key.
Code: Select all
<Border
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="50" Height="50">
<Border.Resources>
<ResourceDictionary>
<ResourceDictionary x:Key="SampleDictionary" Source="basicDictionary.xaml"/>
</ResourceDictionary>
</Border.Resources>
<Border.Background>
<SolidColorBrush Color="{Binding Sample_Blue, Source={StaticResource SampleDictionary}}"/>
</Border.Background>
</Border>
Code: Select all
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="Sample_Blue">#FF0000FF</Color>
</ResourceDictionary>
Code: Select all
<SolidColorBrush Color="{Binding Sample_Blue, Converter="ResArgConverter", ConverterParameter={StaticResource SampleDictionary}}"/>
Code: Select all
NsBool TryConvert(Noesis::Core::BaseComponent* value,
const Noesis::Core::Type* targetType,
Noesis::Core::BaseComponent* parameter,
Noesis::Core::Ptr<Noesis::Core::BaseComponent>& result)
{
Noesis::Core::BaseComponent* pResult = nullptr;
if (nullptr != value)
{
Noesis::Gui::ResourceDictionary* pParam = NsStaticCast<Noesis::Gui::ResourceDictionary*>(parameter);
const NsChar* strKey = value->ToString().c_str();
Noesis::Core::Ptr<Noesis::Gui::ResourceKeyString> pResKeyStr =
Noesis::Gui::ResourceKeyString::TryCreate(strKey);
if (nullptr != pParam && 0 != pResKeyStr && pParam->Find(pResKeyStr.GetPtr(), pResult))
{
result.Reset(pResult);
return true;
}
}
result.Reset();
return false;
}
What am I doing wrong? Is there a better way to perform what I'm trying to do?
Thanks,
Alexandre.