简体   繁体   English

ListBox显示集合名称而不是值

[英]ListBox showing collection name instead of value

Working with C# on a Windows Phone 8.1 project, I'm using SQLite to store some remote connection data, like IP, Name and Port, for a remote control of MPC. 我在Windows Phone 8.1项目上使用C#,我正在使用SQLite存储一些远程连接数据,例如IP,名称和端口,以对MPC进行远程控制。

The XAML has a ListBox element and it should show in each item the connection IP, but instead it is showing the full namespace of it and no valid value. XAML具有一个ListBox元素,它应该在每个项目中显示连接IP,但是它显示了它的完整名称空间,没有有效值。

I may not understood yet how databinding works with Windows Phone and any direction will be welcome. 我可能还不了解数据绑定如何与Windows Phone一起使用,并且欢迎任何方向。 Below, classes and the XAML: 下面是类和XAML:

Models.Connection.cs 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 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 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;
    }

模拟器截图

You placed a ListViewItem directly in your ListBox. 您将ListViewItem直接放置在ListBox中。 What you actually wanted to do was to set the ItemsTemplate 您实际要做的是设置ItemsTemplate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM