I am using MasterDetailsView control for my app. It offers a way to customize the details panel when no items are selected, but it doesn't offer a way to customize the master panel when no items are available.
Basically, what I want to accomplish is to display a message (TextBlock
) instead of the default ListView
when the latter has no items.
I just can't get it to work. My guess is that the ListView
is nested inside a ControlTemplate
that defines the MasterDetailsView
control.
The only way I managed to do this statically (without any update at runtime) was to overwrite MasterDetailsView.Resources
, where I add a Controltemplate
for ListView
, like in the markup below:
<Page
...
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls
...>
<controls:MasterDetailsView.Resources>
<Style TargetType="ListView" >
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="ListView">
<Grid>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Style="{StaticResource SubtitleTextBlockStyle}"
TextAlignment="Center"
Text="No content"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</controls:MasterDetailsView.Resources>
However, as I mentioned, that gives me the static behavior. I need this TextBlock
to be displayed only when the data source my ListView
is bound to runs out of items.
I tried to solve this by binding the Visibility
property of my TextBlock
to a Converter, but the converter isn't even reached (I debugged after adding a breakpoint to the Convert()
method). I'm not sure if I used it properly though (I ommited the declaration of VisibleWhenZeroConverter
and its source code for brevity):
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Style="{StaticResource SubtitleTextBlockStyle}"
TextAlignment="Center"
Text="No content"
Visibility="{Binding ElementName=MyMasterDetailsView, Path=ViewModel.SampleItems.Count, Converter={StaticResource VisibleWhenZeroConverter}}"/>
Obs: ViewModel
is a property of MyMasterDetailsView
, and it has a ObservableCollection
, named SampleItems
, which is never null
.
I also tried to work this out by using DataTriggerBehavior
with a ChangePropertyAction
(from Microsoft.Xaml.Interactions.Core), but without any luck either. I'm also not sure if I did it the right way.
In case someone can answer me if this is even possible with MasterDetailsView
control, I'd appreciate. Or maybe give an example of how I'd do this using one of the approaches from above, or even another one.
Best regards!