0

I'm trying to find a way to change the content of a window in WPF without having to load a new window. In most cases, the following works just fine:

dim x as new window 
x.show()
me.close 

Is there a way to do something like the following?

dim x as new window 
me.content = x 
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
SeanH
  • 584
  • 3
  • 18

4 Answers4

1

This answer is a bit delayed. I ran into a situation where I needed to change all or part of the contents of a window, so as suggested by wpf-it - I used ContentControl.

The window I used this in is being used with a barcode scanner. We have a handful of item types that can be scanned with a different set of options and displays needed for each. Since our app is written in the MVVM pattern (Or at least... MVVM-esque ;) ) I created some XAML:

<ContentControl Grid.Row="2" Content="{Binding itemOptions}" /> 

Which is then bound to this property:

    Public ReadOnly Property itemOptions As UserControl
        Get
            Select Case SearchResult.GetType()
                ' Part
                Case GetType(partHeaderModel)
                    Try
                        Return New partOptions
                    Catch ex As Exception
                        ' Fail
                        ' Return New noResults
                    End Try

                    ' Bin
                Case GetType(binModel)
                    Try
                        Return New binOptions
                    Catch ex As Exception
                        ' Fail
                        Return New noResults
                    End Try

                    ' Rack
                Case GetType(rackModel)
                    Try
                        Return New rackOptions
                    Catch ex As Exception
                        ' Fail
                        Return New noResults
                    End Try

                Case Else
            End Select

            Return New noResults

        End Get
    End Property

I have a search function which returns a data model based on the type of item the user scans. Based on the type, the select statement returns the property user control. I use INotifyPropertyChanged along with the scan event to get the window to update.

SeanH
  • 584
  • 3
  • 18
0

Check this topic: How to get reference to element in resources, WPF?

Read, that it can help .. There are thousands of ways to work, so you must find his or better. An old concept which was used is the MDI: http://www.codeproject.com/Articles/22927/Multiple-Window-Interface-for-WPF

Community
  • 1
  • 1
J. Lennon
  • 3,311
  • 4
  • 33
  • 64
0

How about setting all the possible configurations of your windows in one window, and toggling the visibilities of whatever you want to show and not show as and when needed?

That will make it easier to customize rather than manually modifying the Visual Tree. However remember to keep any panels virtualized or your performance could take a hit.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
SalGad
  • 2,991
  • 2
  • 18
  • 25
0

Did you explore ContentPresenter / ContentControl option in WPF? Keep a content presenter / content control in your window, and set their Content property dynamically... this way you will not need to reload the window.

WPF-it
  • 19,625
  • 8
  • 55
  • 71
  • I'm at home now, so I don't have an exact copy of the code I used. But content control did seem to be it. I did something like this, where content control was named controlContainer: dim x as contentContainer.children.clear contentContainer.children.add(x) That seemed to work like a charm! – SeanH Jun 28 '12 at 01:24