0

In my UWP app mainpage xaml I try to bind an ObservableCollection<BluetoothLEDevice> BleDeviceList to a listview.
If I run my app I get the following error:

System.InvalidCastException: Unable to cast object of type 'Windows.Devices.Bluetooth.BluetoothLEDevice' to type 'UWPsimpleBLE_exampleWithSomeControls.MainPage'. at UWPsimpleBLE_exampleWithSomeControls.MainPage.MainPage_obj2_Bindings.SetDataRoot(Object newDataRoot) at UWPsimpleBLE_exampleWithSomeControls.MainPage.MainPage_obj2_Bindings.ProcessBindings(Object item, Int32 itemIndex, Int32 phase, Int32& nextPhase)

 <Page.Resources>
    <DataTemplate x:Key="ListDataTemplate" x:DataType="local:MainPage">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
            <TextBlock Text="{x:Bind Path=BleDevice.Name }" HorizontalAlignment="Center" Width="150" />
            <StackPanel Margin="20,20,0,0">
                <!--<TextBlock Text="{x:Bind BleDevice.BluetoothAddress.ToString()}" HorizontalAlignment="Left" FontSize="16" />-->
                <!--<TextBlock Text="{x:Bind BleDevice.ConnectionStatus}" HorizontalAlignment="Left" FontSize="10" />-->
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</Page.Resources>



 <ListView HorizontalAlignment="Stretch" Height="100" Margin="0,0,0,0"
                  VerticalAlignment="Top" Background="#FFDED7D7" 
                  BorderBrush="#FFF88686" Foreground="Black" 
                  ItemsSource="{x:Bind BleDeviceList}"
                  ItemTemplate="{StaticResource ListDataTemplate }">
        </ListView>  

If i comment out the line TextBlock Text.. The error is gone,so I must be doing something wrong with my binding

9
  • 2
    You set x:DataType="local:MainPage". And that is why the error message says that it cannot be converted... Have you tried x:DataType with BluetoothLEDevice? Commented May 27, 2018 at 16:46
  • This one here may help: learn.microsoft.com/en-us/windows/uwp/data-binding/… (see "DataTemplate and x:DataType") Commented May 27, 2018 at 16:52
  • @gregkalapos tryed that, but the only possible are app and mainpage. Commented May 27, 2018 at 17:00
  • Did you import the namespace of the class with "xmlns:..." at the top of the XAML page? Commented May 27, 2018 at 17:05
  • 1
    You should import Windows.Devices.Bluetooth.BluetoothLEDevice, not the MainPage. Although I'm not sure if it's a good idea to bind BluetoothLEDevice directly... maybe a class that only contains BluetoothAddress and ConnectionStatus (...plus what you need) would be better. But anyway.. as a first try I'd suggest importing the BluetoothLEDevice and change x:DataType to that. Then you can refine it... Commented May 27, 2018 at 17:56

1 Answer 1

1

As discussed in the comments the solution was to change the x:DataType from MainPage to BluetoothLEDevice class. Additionally the BluetoothLEDevice class also had to be imported. In case of x:Bind you have to define the type of the class that you bind to and in this case the correct class was BluetoothLEDevice.

So this should be the code which does the job:

<DataTemplate x:Key="ListDataTemplate" x:DataType="local:BluetoothLEDevice">

And this line makes the BluetoothLEDevice class visible within the XAML page:

xmlns:local="using:Windows.Devices.Bluetooth"

This page describes x:Binds with DataTemplates (especially the "DataTemplate and x:DataType" part).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.