【问题标题】:Silverlight: Data Binding: Using of Dependency Properties within a Row of a CollectionSilverlight:数据绑定:在集合的一行中使用依赖属性
【发布时间】:2024-04-18 14:00:02
【问题描述】:

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

 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>

现在的问题是,列表中只显示了集合的最后一项……之前的项只用空格呈现;尽管我很确定(通过调试器)前几行的依赖属性(当它们注册并设置它们的值时)应该具有非初始值。如果我直接引用相应的 CLR 属性 (Function.DisplayName) 一切正常。

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

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

【问题讨论】:

    标签: silverlight-4.0 dependency-properties


    【解决方案1】:

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

    【讨论】: