Dmirty
Topic Author
Posts: 6
Joined: 19 Oct 2022, 19:50

Access an array item (in ResourceDictionary) in xaml

26 Aug 2023, 15:55

Hello there.
I have an array of images, defined in ResourceDictionary, like this:
<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>
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:
        <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>
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)
 
User avatar
sfernandez
Site Admin
Posts: 2910
Joined: 22 Dec 2011, 19:20

Re: Access an array item (in ResourceDictionary) in xaml

30 Aug 2023, 11:05

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:
<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>
Then you can use it in your window like this:
<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>
Would something like that work for you?

By the way, this binding syntax is incorrect:
{Binding Source={StaticResource NotificationIcons[0]}}
To reference elements in a collection you should specify it in the Path of the binding:
{Binding Path=[0], Source={StaticResource NotificationIcons}}
 
Dmirty
Topic Author
Posts: 6
Joined: 19 Oct 2022, 19:50

Re: Access an array item (in ResourceDictionary) in xaml

30 Aug 2023, 18:13

Thank you for the answer.
Arrays are not supported in Noesis: #668, so this approach won't work.
Access denied.
Would something like that work for you?
The main question is: how can I change it using bindings? When I create the window, I want show one of the available icons.
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?)
By the way, this binding syntax is incorrect:
Ah, thank you! I tried to google, but without success.
 
User avatar
sfernandez
Site Admin
Posts: 2910
Joined: 22 Dec 2011, 19:20

Re: Access an array item (in ResourceDictionary) in xaml

04 Sep 2023, 17:14

Sorry, the issue #668 was marked as private (I made it public) and I didn't notice.
how can I change it using bindings?
What are you trying to specify in the binding? The key of the icon resource to show in the window?
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.
<Path Data="{Binding NotificationIcon, Converter={StaticResource IconResourceConverter}}">
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;
  }
  ...
}
With triggers you can usa a style like this:
<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>
<Path Style="{StaticResource IconPathStyle}"...>
 
Dmirty
Topic Author
Posts: 6
Joined: 19 Oct 2022, 19:50

Re: Access an array item (in ResourceDictionary) in xaml

04 Sep 2023, 17:53

With a converter you will provide the key in the binding, and the converter will access the application resources to get the specified resource.
I think this is what I needed. I will try. Thank you!

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests