在Silverlight/WPF中绑定复杂属性

gsn*_*erf 9 c# data-binding silverlight xaml silverlight-2.0

假设我有一个自定义数据类型,如下所示:

public class MyDataType
{
  public string SimpleProp1;
  public string SimpleProp2;
  public List<SomeType> ComplexProp;
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个动态创建的数据绑定控件(即ItemsControl或DataGrid).如何在xaml代码中定义的绑定看起来像访问复杂属性的子属性?我认为应该看起来像这样:

<TextBox Text="{Binding simpleSubProp, path=ComplexProp[0]}" />
Run Code Online (Sandbox Code Playgroud)

要么

<TextBox Text="{Binding path=ComplexProp[0].simpleSubProp}" />
Run Code Online (Sandbox Code Playgroud)

但是这两个都给了我xml解析错误.它应该如何看起来正确?是否有可能以某种方式引用集合属性的特定项目?如果不是,我还有其他选择吗?

编辑,这个场景似乎不够清晰:

我有一个

IEnumberable<MyDataType>
Run Code Online (Sandbox Code Playgroud)

绑定到ItemsControl,在DataTemplate内部我有多个TextBox,需要引用复杂属性List中对象的子属性.

sip*_*wiz 7

看起来像属性路径中的Silverlight 索引器中的 poperty路径索引器被破坏了.绕过它的方法是在帖子中建议并使用IValueConverter.

XAML

<UserControl x:Class="Silverlight.Mine.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:sys="System"
  xmlns:sm="clr-namespace:Silverlight.Mine;assembly=Silverlight.Mine"
  Width="400" Height="300">
    <UserControl.Resources> 
       <sm:SomeTypeConverter x:Key="MySomeTypeConverter" />
    </UserControl.Resources>    
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="myTextBlock" Text="{Binding Path=SomeDates, Converter={StaticResource MySomeTypeConverter}}" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

C#Page.xaml.cs

namespace Silverlight.Mine
{
    public partial class Page : UserControl
    {
        private SomeType m_mySomeType = new SomeType();

        public Page()
        {
            InitializeComponent();
            myTextBlock.DataContext = m_mySomeType;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

C#SomeType.cs

namespace Silverlight.Mine
{
    public class SomeType
    {
        public List<DateTime> SomeDates { get; set; }

        public SomeType()
        {
            SomeDates = new List<DateTime>();
            SomeDates.Add(DateTime.Now.AddDays(-1));
            SomeDates.Add(DateTime.Now);
            SomeDates.Add(DateTime.Now.AddDays(1));
        }
    }

    public class SomeTypeConverter : IValueConverter
    {
        public object Convert(object value,
                       Type targetType,
                       object parameter,
                       CultureInfo culture)
        {
            if (value != null)
            {
                List<DateTime> myList = (List<DateTime>)value;
                return myList[0].ToString("dd MMM yyyy");
            }
            else
            {
                 return String.Empty;
            }
        }

        public object ConvertBack(object value,
                              Type targetType,
                              object parameter,
                              CultureInfo culture)
        {
            if (value != null)
            {
                return (List<DateTime>)value;
            }
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)