Silverlight:数据绑定:在集合的行中使用依赖项属性

时间:2011-05-25 12:06:40

标签: silverlight-4.0 dependency-properties

我有一个集合+它的结构:

 public class FunctionListBindStructure : AttributeBase
{
    public FunctionListBindStructure() : base(true) {  }

    //this represents one row of the collection
    public MyFunction Function { get; set; }
    public string Name { get; set; }

}

public class FunctionListBind : AttributeListBase
{
    //this represents 
    public ObservableCollection<FunctionListBindStructure> FunctionList { get; set; }

    public FunctionListBind()
        : base(true)
    {
        FunctionList = new ObservableCollection<FunctionListBindStructure>();
    }

    public override IList GetList()
    {
        return FunctionList as IList;
    }
}

此类使用框架,该框架为CLR属性Function.DisplayName生成依赖属性为“FunctionDisplayNameProperty”。

在我的示例视图中,我将此集合绑定到ListBox

ListBox ItemsSource="{Binding MyModel.FunctionListBind.FunctionList}" Height="52" HorizontalAlignment="Left" Margin="136,157,0,0" Name="listBox1" VerticalAlignment="Top" Width="130" >
        <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                    <TextBlock Text="{Binding FunctionDisplayNameProperty,  Mode=TwoWay}"/>
            </StackPanel>
        </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

现在的问题是,只有集合的最后一项才会显示在列表中...之前的项目仅使用空格呈现;虽然我很确定(通过调试器)前一行的dependendy属性(当它们注册时及其值集)应具有非初始值。如果我直接引用相应的CLR属性(Function.DisplayName),一切正常。

我的问题:我在这里设计错误了吗? Dependency Properties不应该用作行类型吗?我使用相同的模式进行非收集,并且它有效。这也是我想对集合使用相同方法的原因(我可以使用90%的exisitng代码行来生成和设置Dependeny属性)。

感谢任何提示以及如何(如果不是设计错误)调试依赖项属性绑定。

1 个答案:

答案 0 :(得分:0)

这只是我的框架编码的一个错误。我已将依赖项属性定义为实例属性,而不是静态属性。现在一切正常。