C# 绑定到类中的列表

C# 绑定到类中的列表,c#,xaml,listview,uwp,uwp-xaml,C#,Xaml,Listview,Uwp,Uwp Xaml,对于我正在开发的应用程序,我正在使用蓝牙设备进行即时通讯。 每个设备都有一个名称和多个服务。我试图以编程的方式展示它们和它们的服务 为此,我创建了一个包含服务列表的类。但是,当尝试绑定时,服务列表似乎完全为空 XAML: 公共类蓝牙设备 { 公共字符串名称{get;set;} //公共列表服务{get;set;} 公共列表服务{get;set;} } XAML: c#: adapter.ScanForBroadcasts(异步(IBlePeripheral外围设备)=> { //列表服务=

对于我正在开发的应用程序,我正在使用蓝牙设备进行即时通讯。 每个设备都有一个名称和多个服务。我试图以编程的方式展示它们和它们的服务

为此,我创建了一个包含服务列表的类。但是,当尝试绑定时,服务列表似乎完全为空

XAML:

公共类蓝牙设备
{
公共字符串名称{get;set;}
//公共列表服务{get;set;}
公共列表服务{get;set;}
}
XAML:


c#:

adapter.ScanForBroadcasts(异步(IBlePeripheral外围设备)=>
{
//列表服务=新列表();
列表服务=新列表();
字符串devicename=peripheral.advision.devicename;
var_services=peripheral.advision.services;
var servicedata=peripheral.advision.servicedata;
//服务=(_services.ToList());
等待Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
服务。添加(“我是字符串”);
添加(新的BluetoothDevices{Name=devicename,Services=Services});
});
//BluetoothDevices=新的ObservableCollection(BluetoothDevices.Distinct());
}时距秒(30));

像这样更改绑定。我假设您希望此内部
列表视图
显示所有可用的服务。为此,您必须将
服务
列表绑定到
列表视图
项目资源
,以便列表中的每个项目都是
列表视图
中的一个元素。通过使用
Text=“{Binding}”
,文本块直接绑定到集合的每个实例(在这种情况下,
字符串
直接绑定到
文本
)。因此,请按如下方式修改绑定:

    <ListView ItemsSource="{Binding Services}">
        <ListView.ItemTemplate>
             <DataTemplate>
                 <TextBlock Text="{Binding}"/>
             </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

希望这有帮助


编辑:添加了绑定解释

使用
可观察收集
而不是
列表
<代码>列表在向其中添加项目时不会通知任何人,因此在创建UI后不会看到任何添加的内容<代码>可观察采集执行。如果这不能解决问题,那么您可能是绑定错误了。
BluetoothDevices
是您的viewmodel还是其他什么?它应该实现
INotifyPropertyChanged
。还要注意degant的正确建议。我认为这不会是问题所在,因为我正在创建列表,然后将该项添加到observablecollection。编辑:即使使用嵌套的observablecollection,它也不起作用。什么observablecollection?“嵌套的可观察集合”是什么意思?你是说
BluetoothDevices
是一个可观察的集合吗?顺便说一句,假设你对问题原因的假设中至少有一个是错误的总是安全的——否则你就不会在这里寻求帮助了。无论如何,如果你遗漏了重要的细节,人们不得不猜测应该排除什么。如果
BluetoothDevices
是一个
ObservableCollection
并且
ListView
正确绑定到它,请向我们展示该代码,因为当有人说“空ListView”时,第一个问题总是“你是如何填充它的?”我指的是一个嵌套在另一个内部,由于observablecollection是一种类型,而列表是实现它的类型的一部分,因此我以前曾尝试将其绑定到服务本身,但没有成功。如果我理解正确,这里的绑定是指绑定为itemssource的对象?是的
服务
必须绑定到
itemssource
,才能使
列表视图
正常工作。我在回答中也添加了一些解释。非常感谢,MSDN文档对此不是很清楚。没问题。很高兴我能帮忙!:)
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name}" Width="150" />
                    <ListView ItemsSource="{Binding}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Services}"/>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
        adapter.ScanForBroadcasts(async (IBlePeripheral peripheral) =>
        {
            //List<Guid> services = new List<Guid>();
            List<string> services = new List<string>();

            string devicename = peripheral.Advertisement.DeviceName;
            var _services = peripheral.Advertisement.Services;
            var servicedata = peripheral.Advertisement.ServiceData;
            //services = (_services.ToList());


            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =>
            {
                services.Add("i am a string ");
                BluetoothDevices.Add(new BluetoothDevices { Name = devicename, Services = services });
            });
            //BluetoothDevices = new ObservableCollection<BluetoothDevices>(BluetoothDevices.Distinct());
        }, TimeSpan.FromSeconds(30));
    <ListView ItemsSource="{Binding Services}">
        <ListView.ItemTemplate>
             <DataTemplate>
                 <TextBlock Text="{Binding}"/>
             </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>