[英]Data Binding troube using XML serialization and BindingList<string>
Hi I am new to WPF C# and I am converting winform application to WPF and I am having difficulty in data binding( I do not have good idea about binding). 嗨,我是WPF C#的新手,我正在将Winform应用程序转换为WPF,并且在数据绑定方面遇到困难(我对绑定没有很好的想法)。 I am getting the error message :
我收到错误消息:
Binding Expression Path error: Classes property not found on 'object' ' ' String' HashCode = "" target element is 'TextBlock'(Name =' ');
绑定表达式路径错误:在'object'''String'HashCode =“”目标元素上找不到Classes属性是'TextBlock'(Name =''); Target property is Text(type 'string')
目标属性为Text(类型为“字符串”)
Please help. 请帮忙。 thanks
谢谢
I am using XML file and storing a sequence which contains classes( list of string values, like rotate, drag), list of coordinate points and output value.this is my database class. 我正在使用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 is somethink like this--------------- 顺序是这样的---------------
Sequence sample = new Sequence()
{
Classes = Classes,
SourcePath = sequence,
Output = classIndex
};
Now I have a combobox which take value from BindingList Classes and datagrid which take value from Sequence. 现在,我有了一个组合框,它从BindingList类中获取值,而datagrid则从Sequence中获取值。 XML file is
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>
I am binding these two value in mainWindow like this. 我像这样在mainWindow中绑定这两个值。
database = new Database();
cbClasses.ItemsSource = database.Classes;
gridSamples.DataContext = database.Samples;
I think you might need to take a step back and read some tutorials on WPF binding, specifically with the concepts of: * Implementing INotifyPropertyChanged * MVVM pattern 我认为您可能需要退后一步,阅读一些有关WPF绑定的教程,特别是以下概念:*实现INotifyPropertyChanged * MVVM模式
I suggest you take a look at this link which provides a very nice overview (with downloadable source code). 我建议您看一下此链接 ,该链接提供了非常不错的概述(带有可下载的源代码)。 You can also check my answer to another question, which demonstrates some very basic data binding techniques.
您还可以检查我的回答另一个问题时,这表明一些非常基本的数据绑定技术。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.