列表框显示集合名称而不是值
ListBox showing collection name instead of value
在 Windows Phone 8.1 项目中使用 C#,我使用 SQLite 存储一些远程连接数据,如 IP、名称和端口,用于远程控制 MPC。
XAML 有一个 ListBox 元素,它应该在每个项目中显示连接 IP,但它显示的是它的完整命名空间并且没有有效值。
我可能还不明白数据绑定如何与 Windows Phone 一起工作,欢迎任何指导。下面,类 和 XAML:
Models.Connection.cs
namespace RemotyMPC81.Models
{
class Connection
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
private string _Host;
public string Host
{
get
{
return this._Host;
}
set
{
this._Host = value;
}
}
[MaxLength(20)]
public string Name { get; set; }
public string Port { get; set; }
public async static Task<ObservableCollection<Connection>> FetchAllAsList()
{
var rawList = await DatabaseManagement.ConnectionDb().Table<Connection>().ToListAsync();
ObservableCollection<Connection> connections = new ObservableCollection<Connection>();
foreach (Connection connection in rawList){
connections.Add(connection);
}
return connections;
}
public async static Task Insert(Connection connection)
{
await DatabaseManagement.Insert(connection);
}
}
}
Views.Connections.ListPage.xaml
<Grid>
<ListView x:Name="LstConnection" HorizontalAlignment="Left" Height="486" Margin="10,84,0,0" VerticalAlignment="Top" Width="380" ItemsSource="{Binding}">
<ListViewItem>
<TextBlock Text="{Binding Path=Host, Mode=OneWay}" Width="380" Height="39"></TextBlock>
</ListViewItem>
</ListView>
</Grid>
Views.Connections.ListPage.cs
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
ObservableCollection<Connection> connections = await Connection.FetchAllAsList();
LstConnection.ItemsSource = from conn in connections select conn;
}
您直接在列表框中放置了一个 ListViewItem。您真正想要做的是设置 ItemsTemplate
<ListView ...>
<ListView.ItemsTemplate>
<DataTemplate>
<TextBlock ... />
</DataTemplate>
</ListView.ItemsTemplate>
</ListView>
在 Windows Phone 8.1 项目中使用 C#,我使用 SQLite 存储一些远程连接数据,如 IP、名称和端口,用于远程控制 MPC。
XAML 有一个 ListBox 元素,它应该在每个项目中显示连接 IP,但它显示的是它的完整命名空间并且没有有效值。
我可能还不明白数据绑定如何与 Windows Phone 一起工作,欢迎任何指导。下面,类 和 XAML:
Models.Connection.cs
namespace RemotyMPC81.Models
{
class Connection
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
private string _Host;
public string Host
{
get
{
return this._Host;
}
set
{
this._Host = value;
}
}
[MaxLength(20)]
public string Name { get; set; }
public string Port { get; set; }
public async static Task<ObservableCollection<Connection>> FetchAllAsList()
{
var rawList = await DatabaseManagement.ConnectionDb().Table<Connection>().ToListAsync();
ObservableCollection<Connection> connections = new ObservableCollection<Connection>();
foreach (Connection connection in rawList){
connections.Add(connection);
}
return connections;
}
public async static Task Insert(Connection connection)
{
await DatabaseManagement.Insert(connection);
}
}
}
Views.Connections.ListPage.xaml
<Grid>
<ListView x:Name="LstConnection" HorizontalAlignment="Left" Height="486" Margin="10,84,0,0" VerticalAlignment="Top" Width="380" ItemsSource="{Binding}">
<ListViewItem>
<TextBlock Text="{Binding Path=Host, Mode=OneWay}" Width="380" Height="39"></TextBlock>
</ListViewItem>
</ListView>
</Grid>
Views.Connections.ListPage.cs
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
ObservableCollection<Connection> connections = await Connection.FetchAllAsList();
LstConnection.ItemsSource = from conn in connections select conn;
}
您直接在列表框中放置了一个 ListViewItem。您真正想要做的是设置 ItemsTemplate
<ListView ...>
<ListView.ItemsTemplate>
<DataTemplate>
<TextBlock ... />
</DataTemplate>
</ListView.ItemsTemplate>
</ListView>