How to populate a List <string> with StaticResource strings

in my WPF project in C # 4.0, in the resource dictionary I have string resources:

    <System:String x:Key="s_one">One</System:String>
    <System:String x:Key="s_two">Two</System:String>

I want to use the lines above to populate the list of Ls lines in the xaml file.

    <cc:XYZ.Ls>   
       <StaticResource ResourceKey="s_one" />   
       <StaticResource ResourceKey="s_two" /> 
    </cc:XYZ.Ls>

This does not work. Details in the exception {"One" is not a valid value for the "Ls" property. "}

But, when I add another line before these lines, it works well.

    <cc:XYZ.Ls>
      <System.String>Zero</System.String>
      <StaticResource ResourceKey="s_one" />
      <StaticResource ResourceKey="s_two" />
    </cc:XYZ.Ls>

Elements in Ls after launch: {"Zero", "One", "Two"}

Is there a way to insert rows from StaticResource into a row list without adding an extra row to XAML?

Note. Relevant part of the XYZ class:

    public partial class XYZ : UserControl
    {
         public static readonly DependencyProperty LsProperty =
                DependencyProperty.Register("Ls", typeof(List<string>), typeof(XYZ), 
                      new FrameworkPropertyMetadata(new List<string>()));

          public List<string> Ls
          {
             get { return (List<string>)GetValue(XYZ.LsProperty); }
             set { SetValue(XYZ.LsProperty, value); }
           }

          public XYZ()
          {
              InitializeComponent();
              Ls = new List<string>();
          }
    }
+4
source share
1

...

public class ArrayToListConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return new List<string>();
        return ((string[]) value).ToList();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
         throw new NotImplementedException();
    }
}

 public partial class Xyz : UserControl
 {
    public static readonly DependencyProperty LsProperty =
            DependencyProperty.Register("Ls", typeof(List<string>), typeof(Xyz),
                  new FrameworkPropertyMetadata(null));

    public List<string> Ls
    {
        get { return (List<string>)GetValue(Xyz.LsProperty); }
        set { SetValue(Xyz.LsProperty, value); }
    }

    public Xyz()
    {
        InitializeComponent();
    }
  }

  <Window.Resources>

    <x:Array Type="sys:String" x:Key="lst">
        <StaticResource ResourceKey="s_one"/>
        <StaticResource ResourceKey="s_two" />
    </x:Array>

     <wpfApplication2:ArrayToListConvertor x:Key="Conv"></wpfApplication2:ArrayToListConvertor>
</Window.Resources>

<Grid>

    <wpfApplication2:Xyz x:Name="Xyz" Ls="{Binding Path=., Source={StaticResource lst}, Converter={StaticResource Conv}}">

    </wpfApplication2:Xyz>

    <ListBox ItemsSource="{Binding ElementName=Xyz, Path=Ls }">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

+1

Source: https://habr.com/ru/post/1524382/