简体   繁体   中英

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.

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.

I may not understood yet how databinding works with Windows Phone and any direction will be welcome. Below, classes and the 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;
    }

模拟器截图

You placed a ListViewItem directly in your ListBox. What you actually wanted to do was to set the ItemsTemplate

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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