ListBox SelectedItem不更新DataTemplate

时间:2014-02-12 11:10:28

标签: wpf data-binding xsd2code

以下问题:我有一个DataModel,其中包含一个对象列表以及对List中所选对象的引用(以及其他内容) 一切都很好 - 如果我选择s.th.在ListBox中,它也可以在SelectedItem中使用 - 如果我改变了s.th.在SelectedItem上,它在整个DataModel中得到更新。

..但有一个例外:ListBox的内容未更新。我怀疑它与DataTemplate有关,因为我可以观察到以下内容:

  • 如果我更改SelectedItem,DataModel中的项目列表会相应更新(在调试器上检查 - 我也总是在所选项目编辑框中看到正确的数据)
  • 如果我从DataModel中的列表中添加一个对象,ListBox会更新,但是在此更新期间我只在ListBox中获得一个新项目,现有文本没有得到更新(因此List实际上反映了来自的DataModel)
  • 如果我重新加载DataModel,整个ListBox会重建,并且显示的数据也是正确的(因此绑定源没有任何问题)

更新:有关确切问题的新信息

问题实际上是Xsd2Code和ComplexType Extensions(在XSD中)的某种组合。我认为这不是Xsd2Code中的错误,生成的代码看起来很好。

使用的XSD文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="SampleRoot">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="30">
                <xs:element name="SampleElement">
                    <xs:complexType>
                        <xs:complexContent>
                            <xs:extension base="SampleElement"/>
                        </xs:complexContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="SampleElement">
        <xs:attribute name="Name" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

使用过的DataModel(简化,PropertyChanged正确实现,类SampleRootSampleElementSampleRoot由Xsd2Code生成):

public class DataModel : INotifyPropertyChanged
{
    public SampleRootSampleElement SelectedItem;
    public SampleRoot Root;
}

至于XAML,这里没什么特别的:

    <ListBox Height="211" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBoxNames" VerticalAlignment="Top" Width="189" ItemsSource="{Binding Root.SampleElement}" SelectedItem="{Binding SelectedItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBox HorizontalAlignment="Left" Margin="12,229,0,0" Name="textBoxName" VerticalAlignment="Top" Width="189" Text="{Binding SelectedItem.Name}" />

此组合显示了所描述的行为。 要解决此问题,我可以删除XSD中SampleElement的扩展名,这会将XSD缩小为:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="SampleRoot">
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="30">
                <xs:element name="SampleElement" type="SampleElement"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="SampleElement">
        <xs:attribute name="Name" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

其他所有内容保持不变,但没有生成SampleRootSampleElement类(前面是Extension)。而是直接使用SampleElement

public class DataModel : INotifyPropertyChanged
{
    public SampleElement SelectedItem;
    public SampleRoot Root;
}

这一些变化使一切都按预期工作 - 缺点是无法使用扩展。那么,如果我想扩展复杂类型,第一种方法有什么问题呢?

1 个答案:

答案 0 :(得分:0)

由于两个可能但相关的问题,

TextBlock值未更新:

  • 您的Asset(不知道什么是基础类型)类没有实现INotifyPropertyChanged接口。
  • 如果您已在PropertyChanged setter中实施Name事件处理程序 - 使用

public string Name 
{ 
    get
    {
        return this.name;
    }

    set
    {
        this.name = value;
        this.OnPropertyChanged("Name");
    }
}

在应用这两个建议后,您的示例将按预期运行。

相关问题