xaml - Avalonia `ContentControl` not loading the `DataTemplate` - Stack Overflow

admin2025-04-15  1

I have two views, DeviceListView & LoadingView, that are present in LocateDeviceView like this:

<UserControl xmlns=";
             xmlns:x=";
             xmlns:d=";
             xmlns:mc=";
             xmlns:vm="clr-namespace:RemoteMouse.ViewModels.Mobile"
             xmlns:mobileViews="clr-namespace:RemoteMouse.Views.Mobile"
             xmlns:commonViews="clr-namespace:RemoteMouse.Views.Common"
             xmlns:converters="clr-namespace:RemoteMouse.Converters"
             mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="450"
             x:Class="RemoteMouse.Views.Mobile.LocateDeviceView"
             x:DataType="vm:LocateDeviceViewModel">
    <Design.DataContext>
        <vm:LocateDeviceViewModel />
    </Design.DataContext>

    <UserControl.Resources>
        <converters:DataLoaderConverter x:Key="DataLoader"> <!-- This is the DataLoader -->
            <converters:DataLoaderConverter.OnLoading> <!-- This displays correctly -->
                <DataTemplate>
                    <commonViews:LoadingView /> <!-- Reusable loading view -->
                </DataTemplate>
            </converters:DataLoaderConverter.OnLoading>

            <converters:DataLoaderConverter.OnData> <!-- But this is not displaying at all -->
                <DataTemplate DataType="{x:Type vm:LocateDeviceViewModel}">
                    <mobileViews:DeviceListView DataContext="{Binding DeviceListViewModel}" /> <!-- View for displaying data -->
                </DataTemplate>
            </converters:DataLoaderConverter.OnData>
        </converters:DataLoaderConverter>
    </UserControl.Resources>

    <DockPanel>
        <Border DockPanel.Dock="Top" Padding="10 10 10 5">
                <!-- Some static content here -->
        </Border>

        <ContentControl ContentTemplate="{Binding IsSearching, Converter={StaticResource DataLoader}}" /> <!-- The content from DataLoader converter will appear here -->
    </DockPanel>
</UserControl>

In the view model of the above view, which is LocateDeviceViewModel, I have the

[Reactive] private DeviceListViewModel? _deviceListViewModel;
[Reactive] private bool _isSearching;

which I set in my LocateDeviceViewModel like this:

private IDisposable LocateDevices(IDeviceLocator deviceLocator)
{
    IsSearching = true;

    return deviceLocator.LocateDevices().Subscribe(SetDevices);
}


private void SetDevices(SsdpDevice[] devices)
{
    DeviceListViewModel = new DeviceListViewModel
    {
        Devices = devices
    };

    IsSearching = false;
}

The view model loads and hides the LoadingView, depending on IsSearching flag, correctly, but doesn't loads the DeviceListView when IsSearching is flipped to false. I have also verified that DeviceListViewModel property is also not null.

转载请注明原文地址:http://www.anycun.com/QandA/1744716759a86638.html