开发者

MVVM WPF replace ListBox with Label when ItemsSource is empty or null

I have this line in WPF (.NET 3.5) :

<ListBox ItemsSource="{Binding Locks}" Style="{DynamicResource FancyListBox}" />

My desired behavior is that w开发者_如何学Pythonhen the property Locks (an ObservableCollection) is either null or contains 0 elements, show a label like the one below instead of the ListBox.

<Label Content="No locks are present" Style="{DynamicResource FancyLabel}" />

The only solution I can think of so far is to bind the Locks property to visibility using a valueconverter (i.e. converters like CollectionToVisibilityShowIfNull, CollectionToVisibilityShowIfNotNull), but I am not sure if that is the best solution.

... Visibility={Binding Locks, Converter={StaticResource CollectiontoVisibilityShowIfNull}} ..

Thanks for any help!


Here's what I usually do:

<Grid>
    <ListBox Name="lstLocks" ItemsSource="{Binding Locks}" Style="{DynamicResource FancyListBox}" />
    <Label Name="lblNoLocks" Content="No locks are present" Style="{DynamicResource FancyLabel}" Visibility="Collapsed" />
</Grid>

...
<DataTrigger Binding="{Binding Locks.Count}" Value="0">
    <Setter TargetName="lstLocks" Property="Visibility" Value="Collapsed" />
    <Setter TargetName="lblNoLocks" Property="Visibility" Value="Visible" />
</DataTrigger>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜