开发者

WPF UI Design with user controls

开发者 https://www.devze.com 2023-02-15 22:43 出处:网络
As a new comer to WPF, I would like to clarify my approach to build a UI in WPF. I am using WPF with MVVM. My approach is to use a main window which contains user controls which can run several levels

As a new comer to WPF, I would like to clarify my approach to build a UI in WPF. I am using WPF with MVVM. My approach is to use a main window which contains user controls which can run several levels deep in the UI tree. For example, I have an editor in a window. Many items can be edited in the editor and the UI for each items are different, but the editor always shows an OK and Cancel button. So the main editor window with OK and Cancel can be shared between several editors. I am designing the app in such a way that the editor user control will just bind the view model for the item which is edited. So when designing the UI for editing individual items OK or Cancel Button is not pulled in, but simply put the item into the main editor which will provide the buttons. I am pretty sure I can handle the commands correctly with WPF command infrastructure.

If I can make it clear with some xaml here it is.Please dont mind the control placement itself, I mean to explain the basic idea of sharing the Common UI across many items.

<UserControl Name="EditorMainWindow"> 
 <Grid>
   <StackPanel>
        <ItemsControl ItemsSource="{Binding ItemToBeEdited}">                
        </ItemsControl>  
       <Button Content="OK" Width="120" Command="{Binding SomethingforOK}" />
       <Button Content="Cancel" Width="120" Command="{Binding SomethingforCancel}"/开发者_运维知识库>         
    </StackPanel>               
 </Grid>
</UserControl>     

The way I am doing it, the user interface tree can go several levels deep. I will be tremendously benefitted, because if the client ask to change the UI in one particular place, I need not got and change it in many places(provided it is shared).

As I am new to WPF I would like to know if there is any catch in this approach. Could you please tell me if this makes sense or not?


You can save yourself a lot of time. Catel already provides such a window:

DataWindow

It is fully ready for MVVM (Catel is also an MVVM framework) which supports dynamic nested user controls. The nice thing is that it also supports error handling out of the box, so as soon as an error occurs in the window template, the user will see the error in the InfoBarMessageControl.

0

精彩评论

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