I'm following some instructions from here:
WPF Window Return Value
I'm showing a user control in my window. The user cont开发者_运维知识库rol contains several controls, when is validated (I mean, the user has entered all the data) I need to show another user control.
I'm using a border like container.
Here's a way to do this. I don't know that it's the best way, but then I also don't know if this way of designing a UI is all that good in the first place. For instance, if my control vanishes when I put valid data into it, how do I get it back if I realize I made a mistake? There's a reason wizards have a "Back" button.
First, create a base view model class that exposes a boolean IsValid
property. (In my example, I'm calling it ValidatingViewModelBase
.) Each of your views will use a view model derived from this class. In each view model, once all of the properties are valid, set IsValid
to true.
Next, create a user control for each view model as you'd do in any MVVM application. The UI will make more sense if they are designed to be the same size.
Now, create a view model that exposes instances of these view models as properties (in my example, they're called Page1
, Page2
, etc.) and create a view that's bound to it:
<Grid>
<Grid.Resources>
<Style x:Key="{x:Type ValidatingViewModelBase}">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsValid}" Value="True">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style>
<local:UserControl4 DataContext="{Binding Page4}"/>
<local:UserControl3 DataContext="{Binding Page3}"/>
<local:UserControl2 DataContext="{Binding Page2}"/>
<local:UserControl1 DataContext="{Binding Page1}"/>
</Grid>
(This assumes that the DataContext
for the grid is already set.)
How this works: The Grid
presents the controls in the same space on the screen, ordered from back to front. Since they're the same size, and they're opaque, only the last one presented is visible.
When the IsValid
property gets set on the Page1
view, the style changes its Visibility
to Hidden
. Now page 2 is visible. This continues until all four pages are valid. Since we're setting Visibility
to Hidden
, and not Collapsed
, the Grid
remains the same size even after all four pages are invisible.
I haven't tested this, so I don't know how bad a time you're going to have with focus issues. When page 1 becomes invisible, you probably want the first control on page 2 to get the keyboard focus. You may need to look at the discussion of logical focus in the Focus Overview for ideas of how to deal with this.
精彩评论