Access an array item (in ResourceDictionary) in xaml
Hello there.
I have an array of images, defined in ResourceDictionary, like this:
I have a window, where I want to show one of that icons, by bindig a variable.
Just for test I tried to get index 0:
But it shows an error XDG-0001. How can I do it right? (I guess it's something trivial, but I'm a newbie in xaml)
I have an array of images, defined in ResourceDictionary, like this:
Code: Select all
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Array x:Key="NotificationIcons" Type="Path">
<Path Name="ErrorIcon" Fill="#FFFF4040">
...
</Path>
<Path Name="InfoIcon" Fill="#ffffff">
...
</Path>
<Path Name="WarningIcon" Fill="#FFFFFF40">
...
</Path>
</x:Array>
</ResourceDictionary>
Just for test I tried to get index 0:
Code: Select all
<StackPanel Orientation="Horizontal">
<Canvas Name="Icon" Width="36" Height="36" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,0">
<ContentControl Content="{Binding Source={StaticResource NotificationIcons[0]}}"/>
</Canvas>
<TextBlock x:Name="Message" VerticalAlignment="Center" Foreground="White" Padding="10,10,10,10" Text="{Binding Message}"/>
</StackPanel>
-
-
sfernandez
Site Admin
- Posts: 2910
- Joined:
Re: Access an array item (in ResourceDictionary) in xaml
Hello,
Arrays are not supported in Noesis: #668, so this approach won't work.
I suggest you store the icons in the dictionary as geometries, and then just assign it to a Path:
Then you can use it in your window like this:
Would something like that work for you?
By the way, this binding syntax is incorrect:
To reference elements in a collection you should specify it in the Path of the binding:
Arrays are not supported in Noesis: #668, so this approach won't work.
I suggest you store the icons in the dictionary as geometries, and then just assign it to a Path:
Code: Select all
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Geometry x:Key="Geometry.ErrorIcon">M0,0 ...</Geometry>
<Geometry x:Key="Geometry.InfoIcon">M10,0 ...</Geometry>
...
</ResourceDictionary>
Code: Select all
<StackPanel Orientation="Horizontal">
<Path Data="{StaticResource Geometry.ErrorIcon}" Fill="#FFFF4040"/>
<TextBlock x:Name="Message" VerticalAlignment="Center" Foreground="White" Padding="10,10,10,10" Text="{Binding Message}"/>
</StackPanel>
By the way, this binding syntax is incorrect:
Code: Select all
{Binding Source={StaticResource NotificationIcons[0]}}
Code: Select all
{Binding Path=[0], Source={StaticResource NotificationIcons}}
Re: Access an array item (in ResourceDictionary) in xaml
Thank you for the answer.
Is it possible at all (variable binding + xaml)? Or it is possible only using code (like a converter, or maybe something else)? (if so, could you show any example, please?)
Access denied.Arrays are not supported in Noesis: #668, so this approach won't work.
The main question is: how can I change it using bindings? When I create the window, I want show one of the available icons.Would something like that work for you?
Is it possible at all (variable binding + xaml)? Or it is possible only using code (like a converter, or maybe something else)? (if so, could you show any example, please?)
Ah, thank you! I tried to google, but without success.By the way, this binding syntax is incorrect:
-
-
sfernandez
Site Admin
- Posts: 2910
- Joined:
Re: Access an array item (in ResourceDictionary) in xaml
Sorry, the issue #668 was marked as private (I made it public) and I didn't notice.
As StaticResource keys cannot use bindings you should use either a Converter or triggers.
With a converter you will provide the key in the binding, and the converter will access the application resources to get the specified resource.
With triggers you can usa a style like this:
What are you trying to specify in the binding? The key of the icon resource to show in the window?how can I change it using bindings?
As StaticResource keys cannot use bindings you should use either a Converter or triggers.
With a converter you will provide the key in the binding, and the converter will access the application resources to get the specified resource.
Code: Select all
<Path Data="{Binding NotificationIcon, Converter={StaticResource IconResourceConverter}}">
Code: Select all
public class IconResourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ResourceDictionary dict = Noesis.GUI.GetApplicationResources();
if (dict.Contains(value)) return dict[value];
return null;
}
...
}
Code: Select all
<Style x:Key="IconPathStyle" TargetType="Path">
<Style.Triggers>
<DataTrigger Binding="{Binding NotificationIcon}" Value="Error">
<Setter Property="Data" Value="{StaticResource Geometry.ErrorIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding NotificationIcon}" Value="Info">
<Setter Property="Data" Value="{StaticResource Geometry.InfoIcon}"/>
</DataTrigger>
...
</Style.Triggers>
</Style>
Code: Select all
<Path Style="{StaticResource IconPathStyle}"...>
Re: Access an array item (in ResourceDictionary) in xaml
I think this is what I needed. I will try. Thank you!With a converter you will provide the key in the binding, and the converter will access the application resources to get the specified resource.
Who is online
Users browsing this forum: Semrush [Bot] and 0 guests