I have a couple of views that I display as overlays on the main window by binding the visibility to a boolean property in my ViewModel.
The main window looks something like this:
<Grid>
<vw:MainContentView/>
<!-- Overlays (normally invisible) -->
<vw:NotificationsView/>
</Grid>
The 'NotificationsView' has a style that looks like this:
<Style x:Key="NotificationsView" TargetType="UserControl">
<!-- snipped -->
<Style.Triggers>
<DataTrigger Binding="{Binding IsNotificationsViewVisible}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsNotificationsViewVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
I am wondering whether it might be preferable to use a Popup with the 'IsOpen' property bound to the 'IsNotificationViewVisible' property in the ViewModel, rather than toggling the visibility of the view directly:
<Grid>
<vw:MainContentView />
<!-- Popups (normally invisible) -->
<Popup IsOpen="{Binding IsNotificationsViewVisible}">
<vw:NotificationItemsView/>
</Popup>
开发者_高级运维 </Grid>
Are there any reasons I'd want to use one approach over the other (memory usage or otherwise)? Both approaches seem to work just fine - the Popup comes with a couple of free animations but otherwise looks the same.
If you're displaying content on an already existing window, you should probably use the Visibility approach. The Popup acts as a mini-window that can initially position itself based on the location of elements in the main window. That means that it can go outside the bounds of the main window and won't move around when you move the main window. It also doesn't effect the layout of other elements in the window. I imagine it also has some overhead associated with this, but I have not run any exact numbers.
As a bonus, I'll throw in a suggestion: you could use a converter to make Visibility binding easier.
精彩评论