【发布时间】: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