列表框显示集合名称而不是值

时间:2015-06-26 22:35:25

标签: c# xaml listbox windows-phone-8.1

在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;
    }

Emulator Screenshot

1 个答案:

答案 0 :(得分:1)

您直接在ListBox中放置了 ListViewItem 。你真正想要做的是设置 ItemsTemplate

<ListView ...>
    <ListView.ItemsTemplate>
        <DataTemplate>
            <TextBlock ... />
        </DataTemplate>
    </ListView.ItemsTemplate>
 </ListView>