0

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

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             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.

4
  • Adding Content={Binding} in <ContentControl Content={Binding} ContentTemplate="{Binding IsSearching, Converter={StaticResource DataLoader}}" /> resolved the issue. I am still trying to understand how it fixed the issue. If anyone has any idea then please comment. Commented Feb 4 at 13:31
  • It is unclear what exactly is difficult to understand here. The Content property was not set, so there was nothing to display. Commented Feb 5 at 12:59
  • What's the difference between Content and ContentTemplate? Commented Feb 8 at 19:10
  • One is the data, the other defines how the data is displayed. There is online documentation available. Commented Feb 8 at 19:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.