我正在尝试找到一种方法来更改 WPF 中窗口的内容,而无需加载新窗口。在大多数情况下,以下工作正常:
dim x as new window
x.show()
me.close
有没有办法做类似下面的事情?
dim x as new window
me.content = x
这个答案有点延迟。我遇到了需要更改窗口的全部或部分内容的情况,因此正如 wpf-it 所建议的那样 - 我使用了 ContentControl。
我在其中使用的窗口正在与条形码扫描仪一起使用。我们有一些项目类型,可以使用一组不同的选项进行扫描,并为每种类型提供所需的显示。由于我们的应用程序是用 MVVM 模式编写的(或者至少...... MVVM-esque ;))我创建了一些 XAML:
<ContentControl Grid.Row="2" Content="{Binding itemOptions}" />
然后绑定到这个属性:
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
我有一个搜索功能,它根据用户扫描的项目类型返回一个数据模型。根据类型,select 语句返回属性用户控件。我使用 INotifyPropertyChanged 和扫描事件来更新窗口。
检查此主题:如何获取对资源中元素的引用,WPF?
阅读,它可以帮助.. 有成千上万的工作方式,所以你必须找到他或更好的。使用的一个旧概念是 MDI: http: //www.codeproject.com/Articles/22927/Multiple-Window-Interface-for-WPF
如何在一个窗口中设置所有可能的窗口配置,并在需要时切换想要显示和不显示的任何内容的可见性?
这将使自定义而不是手动修改可视树更容易。但是请记住将任何面板保持虚拟化,否则您的性能可能会受到影响。
您是否在 WPF 中探索ContentPresenter
/选项?ContentControl
在您的窗口中保留一个内容演示者/内容控件,并Content
动态设置它们的属性......这样您就不需要重新加载窗口。