开发者

In XAML bind Text to x index of static collection with x being supplied by the y index of another collection?

开发者 https://www.devze.com 2023-02-20 00:15 出处:网络
I have a static collection of strings and would like to display one of these strings from a certain index. That index is supplied at run time, and is an integer value in the second index in another co

I have a static collection of strings and would like to display one of these strings from a certain index. That index is supplied at run time, and is an integer value in the second index in another collection.

I can bind to the static collection, but how do I bind the path to the value in the other collection? That is, what do I use as a value to the Path argument in the TextBlock binding?

The code here is just for experimenting and is not part of the working code. It is Visual Studio Designer friendly, and if I get the binding right, it will show Wednesday in the designer without running:

<Window x:Class="BindingCollectionIndex.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:BindingCollectionIndex"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:AnotherClass
        x:Key="Foo" />
</Window.Resources>
<Grid>
    <TextBlock
        Text="{Binding Source={x:Static local:MyStaticCollections.Days},
        Path=[**Wrong ... Foo.CollectionOfIntegers[2]**]}" />
</Grid>

The static class:

           using System.Collections.Generic;

            namespace BindingCollectionIndex
        {
            static class MyStaticCollections
           开发者_如何学Go {
                public static List<string> Days =new List<string> { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
            }
        }

The class that supplies the index value:

using System.Collections.ObjectModel;

namespace BindingCollectionIndex
{
    class AnotherClass
    {
        private ObservableCollection<int> collectionOfIntegers = new ObservableCollection<int>
        {
            1,2,3,4,5
        };
        public ObservableCollection<int> CollectionOfIntegers
        {
            get
            {
                return collectionOfIntegers;
            }

        }
    }

}

There is nothing added to the xaml's code behind.

Thanks for reading. David


Have you considered using a Dictionary?

class DaysViewModel
{
    public IDictionary<int,string> Days { get; set; }

    public DaysViewModel()
    {
        Days = new Dictionary<int, string>
            {
                {1,"Monday"},
                {2,"Tuesday"},
                {3,"Wednesday"},
                {4,"Thursday"},
                {5,"Friday"}
            };

    }
}

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:DaysViewModel x:Key="Week" />
</Window.Resources>
<Grid>
    <TextBox Text="{Binding Source={StaticResource Week},Path=Days[2]}" />
</Grid>

0

精彩评论

暂无评论...
验证码 换一张
取 消