列表视图内的用户控件,windows phone 8.1
Posted
技术标签:
【中文标题】列表视图内的用户控件,windows phone 8.1【英文标题】:Usercontrol inside the listview, windows phone 8.1 【发布时间】:2017-12-05 22:37:19 【问题描述】:我在将歌曲标题和艺术家绑定到列表视图中的 SongView 用户控件时遇到问题,代码如下:StartPage.xaml
<ListView x:Name="PopularSongs" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="Binding Song.Title"/>
<local:SongView
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
Song="Binding"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
StartPage.cs
public StartPage()
this.InitializeComponent();
this.DataContext = this;
vm.NewSongs.Add( new Song()
Artist = "Scorpions",
Title = "Wind of Change",
ImageURL = "http://images.45cat.com/scorpions-wind-of-changed-mercury.jpg",
Length = 2.56,
Size = 3.3
);
vm.NewSongs.Add(new Song()
Artist = "Omega",
Title = "Gyongihau Lany",
ImageURL = "http://img11.nnm.me/b/d/c/b/7/bdcb701dbca19fde7fed545403f769fb_full.jpg",
Length = 5.32,
Size = 12
);
PopularSongs.ItemsSource = vm.NewSongs;
这里,vm.NewSongs 是 Song 类型的 ObservableCollection
SongView.xaml
<UserControl
x:Class="soldo.SongView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:soldo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="60"
d:DesignWidth="300"
x:Name="root">
<Grid Background="StaticResource ExtraLightGreyBrush" Margin="0,3,0,0" HorizontalAlignment="Stretch"> <StackPanel Grid.Column="1" Margin="5">
<TextBlock FontFamily="StaticResource OpenSansRegular"
Text="Binding ElementName=root, Path=Song.Title, FallbackValue=Title"
Foreground="StaticResource BlackBrush"
FontSize="24"/>
<TextBlock FontFamily="StaticResource OpenSansBold"
Text="Binding ElementName=root, Path=Song.Artist, FallbackValue=Artist"
FontSize="15"
Margin="0,-5,0,0"
Foreground="StaticResource OrangeBrush"/>
</Grid>
</UserControl>
SongView.cs
public static readonly DependencyProperty SongProperty = DependencyProperty.Register(
"Song",
typeof(Song),
typeof(SongView),
new PropertyMetadata(string.Empty)
);
public Song Song
get
return GetValue(SongProperty) as Song;
set
SetValue(SongProperty, value);
输出中有一些绑定错误,但我不知道为什么会出现
【问题讨论】:
什么是绑定错误? 另外,StartPage 上文本块的绑定应该是Binding Path=Title
错误:BindingExpression 路径错误:在 'soldo.Song、soldo、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null' 上找不到 'Title' 属性
输出显示 10 个类似这样的错误,例如“未找到艺术家”属性等。
更改绑定路径=标题没有帮助
【参考方案1】:
在 Windows Phone 上,数据绑定不适用于字段。确保您只绑定属性。
【讨论】:
以上是关于列表视图内的用户控件,windows phone 8.1的主要内容,如果未能解决你的问题,请参考以下文章
Windows Phone 8 用户控件的 VisibleChanged 事件在哪里?
如何检测到 UI 元素(全景和列表框)的数据绑定何时在 windows phone 7 中完成?
如何在 Windows Phone 8.1 的列表视图中更新 <image> 的图像