none
新学WPF,有一个数据绑定的问题 RRS feed

  • 问题

  • 我做了一个窗体,有一个listview和textbox控件,现在是listview与本地xml文档相绑定,然后textbox显示listview选定项的某一列,listview的代码是这样的:

     <ListView x:Name="lstView" Grid.Row="3" VerticalAlignment="Top" Margin="3">
                <ListView.View>
                    <GridView AllowsColumnReorder="True" ColumnHeaderToolTip="文件信息">
                        <GridViewColumn Header="音乐名称" DisplayMemberBinding="{Binding XPath=Title}"/>
                        <GridViewColumn Header="艺术家" DisplayMemberBinding="{Binding XPath=Artist}"/>
                        <GridViewColumn Header="发行时间" DisplayMemberBinding="{Binding XPath=Year}"/>
                        <GridViewColumn Header="唱片集" DisplayMemberBinding="{Binding XPath=Album}"/>
                        <GridViewColumn Header="文件地址" DisplayMemberBinding="{Binding XPath=@FullName}"/>
                    </GridView>
                </ListView.View>
            </ListView>

    textbox的代码:

    <TextBox x:Name="tbxAddress" Text="{Binding ElementName=lstView, Path=SelectedItem,Converter={StaticResource pathConverter}}"/>

    转换器如下:


    class MusicPathConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { XmlElement xElement =( XmlElement) value; return xElement.GetAttribute("FullName"); } /// <summary> /// 未使用 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }

    请教各位高手,为什么会报“未将对象引用设置到对象的实例”的异常呢?哪里出了问题?



    2012年5月21日 3:25

答案