BindingExpression路径错误:'lim'上找不到'username'属性(请帮助) [英] BindingExpression path error: 'username' property not found on 'lim'(pls help)
问题描述
我尝试使用此代码绑定后收到此错误
(任何人都可以帮我解决此错误吗?)
< 网格 x:名称 = ContentGrid 保证金 = 0 ,22,0,-22 Grid.Row = 1 >
< ListBox 高度 = 444 HorizontalAlignment = 左 保证金 = 20,81,0,0 名称 = listBox1 VerticalAlignment = Top 宽度 = 434 >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel 方向 = 水平 >
< TextBlock 文本 = {Binding username} / >
< TextB锁 文字 = {Binding password} / >
< TextBlock < span class =code-attribute> 文本 = {Binding gender} / >
< / StackPanel >
< / DataTemplate >
< / ListBox.ItemTemplate >
< / ListBox >
< / Grid >
错误
System.Windows.Data错误:BindingExpression路径错误:''用户名在''lim''''System.String''找不到属性(HashCode = 339799040)。 BindingExpression:Path =''username''DataItem =''lim''(HashCode = 339799040); target元素是''System.Windows.Controls.TextBlock''(Name =''''); target属性是''Text''(类型''System.String'')..
我的整个项目代码
xaml代码
< phone:PhoneApplicationPage
x:Class = Epromo.MainPage
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
xmlns:phone = clr-namespace:Microsoft.Phone.Controls; assembly = Microsoft.Phone
xmlns:shell = clr-namespace:Microsoft.Phone.Shell; assembly = Microsoft.Phone
xmlns:d = http://schemas.microsoft.com/expression/ blend / 2008
xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006
mc:可忽略 = d d:DesignWidth = 480 d:DesignHeight = 768
FontFamily = {StaticResource PhoneFontFamilyNormal}
FontSize = {StaticResource PhoneFontSizeNormal}
前景 = {StaticResource PhoneForegroundBrush}
SupportedOrientations = 肖像 方向 = Portrait
shell:SystemTray.IsVisible = True >
<! - Layo utRoot是放置所有页面内容的根网格 - >
< 网格 x:名称 = LayoutRoot < span class =code-attribute>背景 = 透明 >
< Grid.RowDefinitions < span class =code-keyword>>
< RowDefinition 高度 = 83 / >
< RowDefinition 高度 = 685 * / >
< / Grid.RowDefinitions > ;
<! - TitlePanel包含应用程序名称和页面标题 - >
< StackPanel x:名称 = TitlePanel 保证金 = 9,17,3,0 > < / StackPanel >
< 按钮 内容 = 正在进行 HorizontalAlignment = 左 保证金 = - 5,85,0,0 VerticalAlignment = Top Grid.RowSpan = 2 宽度 = 195 / >
< 按钮 内容 = 大多数视图 HorizontalAlignment = 左 保证金 = 161,85,0,0 VerticalAlignment = Top Grid.RowSpan = 2 宽度 < span class =code-keyword> = 186 RenderTransformOrigin = 0.5,0.5 / >
< 按钮 内容 = 即将推出 HorizontalAlignment = 左 保证金 = 321, 85,-13,0 VerticalAlignment = Top Grid.RowSpan = 2 < span class =code-attribute>宽度 = 170 FontSize = 20 高度 = 72 / >
< 网格 x:名称 = ContentGrid 保证金 = 0,22,0,-22 Grid.Row = 1 >
< ListBox 高度 = 444 HorizontalAlignment = 左 保证金 = 20,81,0,0 名称 = listBox1 VerticalAlignment = Top 宽度 = 434 >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel 方向 = 水平 >
< TextBlock < span class =code-attribute> 文本 = {Binding username} / >
< TextBlock 文字 = {绑定密码} / >
< TextBlock 文字 = {绑定性别} / >
< / StackPanel >
< / DataTemplate >
< / ListBox.ItemTemplate >
< / ListBox >
< / Grid >
< 按钮 内容 = 注册 FontSize = 17 高度 = 60 名称 = < span class =code-keyword>注册 宽度 = 120 点击 = signup_Click 保证金 = 371,43,-11,647 Grid.RowSpan = 2 / >
< TextBlock x:名称 = ApplicationTitle 样式 = {StaticResource PhoneTextNormalStyle} FontSize = 24 FontFamily = Times New Roman < span class =code-attribute> 高度 = 34 保证金 = 4,0,32,49 >
< 运行 文字 = Epromo / > < / TextBlock >
<! - ContentPanel - 在此处添加其他内容 - >
< ; / Grid >
< / phone:PhoneApplicationPage >
cs code
< pre lang =cs> 使用 Epromo.ServiceReference1;
命名空间 Epromo
{
public partial class MainPage:PhoneApplicationPage
{
private Service1Client _serviceClient;
// 构造函数
public MainPage()
{
InitializeComponent();
已加载+ = 新 RoutedEventHandler(Post_Loaded);
}
void Post_Loaded( object sender,EventArgs e)
{
_serviceClient = new Service1Client();
_serviceClient.ListPostCompleted + = new EventHandler< ListPostCompletedEventArgs>(getList_Completed);
_serviceClient.ListPostAsync();
}
void getList_Completed( object sender,ListPostCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
}
private void signup_Click( object sender,RoutedEventArgs e)
{
NavigationService.Navigate( new Uri( / signupPage.xaml,UriKind.Relative));
}
}
}
修改绑定
< TextBlock Text =" {Binding username}" />
to
< TextBlock Text =" {Binding}" />
并删除其他两个...检查结果!你得到了什么?
提示:你正在将DataTable转换为List,其中每个记录的列组成一个完整的新记录。在你的清单中,例如:
DataTable看起来像:
User1 |密码1 |男性
User2 |密码2 |男性
User3 |密码3 |女性
看起来像:
用户1
密码1
男
用户2
密码2
男
用户3
密码3
女性
ItemsControl现在尝试显示表示为字符串的所有9条记录。对于每个字符串,它应用DataTemplate。绑定到例如username现在尝试在Type String的离子上获取名为username的属性。
另一条线索:修改Binding,使其显示为:
< TextBlock Text =" {Binding Length}" />
(绑定到属性字符串中实际应存在的属性长度)
考虑到这一点,请重新检查绑定错误。所有这些信息都在那里!
我希望我能稍微清理一下
i am getting this error after i try to binding using this code
(can anyone help me with this error?)
<Grid x:Name="ContentGrid" Margin="0,22,0,-22" Grid.Row="1">
<ListBox Height="444" HorizontalAlignment="Left" Margin="20,81,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding username}" />
<TextBlock Text="{Binding password}" />
<TextBlock Text="{Binding gender}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
error
System.Windows.Data Error: BindingExpression path error: ''username'' property not found on ''lim'' ''System.String'' (HashCode=339799040). BindingExpression: Path=''username'' DataItem=''lim'' (HashCode=339799040); target element is ''System.Windows.Controls.TextBlock'' (Name=''''); target property is ''Text'' (type ''System.String'')..
my whole project code
xaml code
<phone:PhoneApplicationPage
x:Class="Epromo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" >
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="83"/>
<RowDefinition Height="685*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Margin="9,17,3,0"></StackPanel>
<Button Content="On going" HorizontalAlignment="Left" Margin="-5,85,0,0" VerticalAlignment="Top" Grid.RowSpan="2" Width="195"/>
<Button Content="most view" HorizontalAlignment="Left" Margin="161,85,0,0" VerticalAlignment="Top" Grid.RowSpan="2" Width="186" RenderTransformOrigin="0.5,0.5"/>
<Button Content="Coming soon" HorizontalAlignment="Left" Margin="321,85,-13,0" VerticalAlignment="Top" Grid.RowSpan="2" Width="170" FontSize="20" Height="72"/>
<Grid x:Name="ContentGrid" Margin="0,22,0,-22" Grid.Row="1">
<ListBox Height="444" HorizontalAlignment="Left" Margin="20,81,0,0" Name="listBox1" VerticalAlignment="Top" Width="434" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding username}" />
<TextBlock Text="{Binding password}" />
<TextBlock Text="{Binding gender}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
<Button Content="sign up" FontSize="17" Height="60" Name="signup" Width="120" Click="signup_Click" Margin="371,43,-11,647" Grid.RowSpan="2" />
<TextBlock x:Name="ApplicationTitle" Style="{StaticResource PhoneTextNormalStyle}" FontSize="24" FontFamily="Times New Roman" Height="34" Margin="4,0,32,49">
<Run Text="Epromo" /></TextBlock>
<!--ContentPanel - place additional content here-->
</Grid>
</phone:PhoneApplicationPage>
cs code
using Epromo.ServiceReference1;
namespace Epromo
{
public partial class MainPage : PhoneApplicationPage
{
private Service1Client _serviceClient;
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Post_Loaded);
}
void Post_Loaded(object sender, EventArgs e)
{
_serviceClient = new Service1Client();
_serviceClient.ListPostCompleted += new EventHandler<ListPostCompletedEventArgs>(getList_Completed);
_serviceClient.ListPostAsync();
}
void getList_Completed(object sender, ListPostCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
}
private void signup_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/signupPage.xaml", UriKind.Relative));
}
}
}
modify the Binding
<TextBlock Text="{Binding username}" />
to
<TextBlock Text="{Binding}" />
and remove the other two...examine the result! what are you getting?
Hint: your are transforming the DataTable to a List, where the columns of each record makes up a complete new "record" in your list, e.g.:
The DataTable looks like:
User1 | Password1 | male
User2 | Password2 | male
User3 | Password3 | female
THe lsit looks like:
User1
Password1
male
User2
Password2
male
User3
Password3
female
The ItemsControl now tries to display all of those 9 records represented as a string. for each string it applies the DataTemplate. The Binding to e.g. "username" now tries to get a property named "username" on an ionstance of Type String.
Another clue: Modify the Binding, so that it reads:
<TextBlock Text="{Binding Length}" />
(Binding to a Property "Length" that should actually exist on type String)
With this in mind, reexamine the Binding-Error. All this Information is in there!
I hope i could clear up things a little bit
这篇关于BindingExpression路径错误:'lim'上找不到'username'属性(请帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!