Dialog form in wpf change content
I want to display a dialog form for new and edit actions... However title, buttons , and few other things should change.
I am wondering how i could implement this. Provide an enum value at co开发者_开发技巧nstructor ? Like Mode.New or Mode.Edit ? Is there a way to avoid writing code like spNewButtons.Visibillity=Collapsed .. etc , and put it inside wpf ?You can bind visibility with your mode
property, and create a specific IValueConverter to convert the mode to a proper Visibility value. ie:
<StakPanel Visibility={Binding Mode,Converter={StaticResource myProperConverter}}></StackPanel>
Usually my WPF dialogs are all ContentControls that get displayed in a Popup.
My code usually looks like this:
<Grid Name="RootPanel">
<!-- Other Content -->
<!-- Popup is always last so it gets displayed on top of other contnet -->
<local:PopupPanel
local:PopupPanel.PopupParent="{Binding ElementName=RootPanel}"
local:PopupPanel.IsPopupVisible="{Binding IsPopupVisible}"
local:PopupPanel.PopupEnterKeyCommand="{Binding SaveCommand}"
local:PopupPanel.PopupEscapeKeyCommand="{Binding CancelCommand}">
<DockPanel>
<!-- Header -->
<Label DockPanel.Dock="Top" Content="{Binding PopupHeader}" />
<!-- Buttons -->
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Save" Command="{Binding PopupSaveCommand}" />
<Button Content="Cancel" Command="{Binding PopupCancelCommand}" />
</StackPanel>
<!-- Actual content displayed is determined by DataTemplates -->
<ContentControl Content="{Binding PopupContent}" />
</DockPanel>
</local:PopupPanel>
</Grid>
I removed a lot of the styles to make this easier to read, but you can see the general idea of how it's put together. My ViewModel usually contains properties for IsPopupVisible
, PopupContent
, and PopupHeader
, and commands for PopupSaveCommand
and PopupCancelCommand
I use my own custom popup in most cases, although the same thing could be done with a WPF popup.
精彩评论