使用XML序列化和BindingList <string> </string>的数据绑定问题

时间:2013-03-13 18:40:04

标签: c# wpf xml-serialization

您好我是WPF C#的新手,我正在将winform应用程序转换为WPF并且我在数据绑定方面遇到困难(我对绑定没有好主意)。 我收到错误消息:

  

绑定表达式路径错误:'object'上找不到类属性''字符串'HashCode =“”目标元素是'TextBlock'(Name ='');目标属性是Text(类型'string')

请帮忙。谢谢

我正在使用XML文件并存储包含类的字符串(字符串值列表,如旋转,拖动),坐标点列表和输出值。这是我的数据库类。

public class Database
    {
     public BindingList<string> Classes { get;  set; }
        public BindingList<Sequence> Samples { get;  set; }


        public Database()
        {
            Classes = new BindingList<string>();
            Samples = new BindingList<Sequence>();
        }

        public void Save(Stream stream)
        {
            var serializer = new XmlSerializer(typeof(BindingList<Sequence>));
            serializer.Serialize(stream, Samples);
        }


        public void Load(Stream stream)
        {
            var serializer = new XmlSerializer(typeof(BindingList<Sequence>));
            var samples = (BindingList<Sequence>)serializer.Deserialize(stream);

            Classes.Clear();
            foreach (string label in samples.First().Classes)
            {
                Classes.Add(label);
                Console.WriteLine(" lets check out the classes ....." + label);
            }

            Samples.Clear();
            foreach (Sequence sample in samples)
            {
                sample.Classes = Classes;
                Samples.Add(sample);
                Console.WriteLine(" lets check out the samples ....." + sample);
            }
        }
}
}

顺序是这样的想法---------------

Sequence sample = new Sequence()
            {
                Classes = Classes,
                SourcePath = sequence,
                Output = classIndex
            };

现在我有了一个组合框,它从BindingList Classes和datagrid获取值,它从Sequence获取值。

是XML文件
<Window x:Class="WpfLearnGUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="417" Width="608">

    <DockPanel Width="Auto" Height="Auto" LastChildFill="True">

        <DataGrid 
            DataContext="{Binding}" ItemsSource="{Binding }"
            AutoGenerateColumns="false" Height="73" 
            Name="gridSamples"  Width="Auto"  HorizontalAlignment="Right"  DockPanel.Dock="Right"  VerticalAlignment="Top" IsEnabled="True">

        <DataGrid.Columns>
            <DataGridTextColumn 
                  Header="Gesture" />
            <DataGridTextColumn 
                    Header="Class" 
                   />
        </DataGrid.Columns>
      </DataGrid>

        <Grid Height="Auto" Name="grid1" Background="Coral" DockPanel.Dock="Bottom" Width="Auto" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="240*"/>
                <ColumnDefinition Width="264*" />

            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*"/>
                 <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="105,0,0,0" Name="cbClasses" VerticalAlignment="Top" Width="120"  DisplayMemberPath="Classes" SelectedValuePath="Classes" IsEditable="True" DataContext="{Binding}" ItemsSource="{Binding}" />

            <Button Content="DataBase" Grid.Column="2" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="53,1,0,0" 
                    Name="button5" VerticalAlignment="Top" Width="75" >
                <Button.ContextMenu>
                    <ContextMenu>
                        <Menu>
                            <MenuItem Header="_Open" IsCheckable="True" Click="Open_click"></MenuItem>
                        </Menu>
                    </ContextMenu>  
                </Button.ContextMenu>
                </Button>
        </Grid> 
    </DockPanel>
</Window>

我在mainWindow中绑定了这两个值。

database = new Database();
cbClasses.ItemsSource = database.Classes;
gridSamples.DataContext = database.Samples;            

1 个答案:

答案 0 :(得分:0)

我认为您可能需要退一步阅读一些有关WPF绑定的教程,特别是以下概念: *实现INotifyPropertyChanged * MVVM模式

我建议你看看这个link,它提供了一个很好的概述(可下载的源代码)。您还可以查看我的answer另一个问题,该问题演示了一些非常基本的数据绑定技术。