【问题标题】:In XAML bind Text to x index of static collection with x being supplied by the y index of another collection?在 XAML 中,将 Text 绑定到静态集合的 x 索引,其中 x 由另一个集合的 y 索引提供?
【发布时间】:2011-03-28 07:48:37
【问题描述】:

我有一个静态的字符串集合,并希望显示某个索引中的其中一个字符串。该索引是在运行时提供的,并且是另一个集合的第二个索引中的整数值。

我可以绑定到静态集合,但是如何将路径绑定到另一个中的值 收藏? 也就是说,我在 TextBlock 绑定中使用什么作为 Path 参数的值?

此处的代码仅用于试验,不是工作代码的一部分。它对 Visual Studio 设计器很友好,如果我正确绑定,它将在设计器中显示星期三而不运行:

<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>

静态类:

           using System.Collections.Generic;

            namespace BindingCollectionIndex
        {
            static class MyStaticCollections
            {
                public static List<string> Days =new List<string> { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
            }
        }

提供索引值的类:

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;
            }

        }
    }

}

xaml 的代码后面没有添加任何内容。

感谢阅读。 大卫

【问题讨论】:

    标签: wpf silverlight binding


    【解决方案1】:

    您是否考虑过使用字典?

    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>
    

    【讨论】:

    • 谢谢,但我设计了这个例子来帮助以简单的方式展示问题,我真正想做的事情更复杂。我可以通过在我的 OP 的 Path 参数中输入“2”来显示“星期三”,但我想要一个关于绑定问题的答案。再次感谢。
    • 对不起,那我真的不明白你要去哪里。也许更具体地描述您的应用程序。您是要从某种列表中选择列表还是从哪里来?
    猜你喜欢
    • 1970-01-01
    • 2021-03-23
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 2019-07-17
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多