我正在使用ReactiveUI
和MahApps.Metro
开始新项目。我遇到的第一个障碍是一个没有完整显示的观点问题。我有一个Window
,在Window
我有一个来自RxUI库的ViewModelViewHost
。它是嵌套视图模型的简单容器。 ActiveItem绑定已正确绑定到用户控件的视图模型,并且用户控件的Button
在屏幕上可见。
我期望看到的是Window
,背景为深灰色,中间有一个按钮。我看到的是Window
的默认背景和中间的Button
。这个不对。如果我删除按钮,我在Window
上看不到任何内容,只看到默认背景。
似乎ViewModelViewHost
仅显示UserControl的实际内容,并且忽略了不被视为真实控件的内容,例如网格等。
之前有没有人遇到过这种行为?
<mah:MetroWindow x:Class="...MainWindow"
xmlns:mah="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:rx="clr-namespace:ReactiveUI;assembly=ReactiveUI"
WindowStartupLocation="CenterScreen"
ShowTitleBar="False"
ShowCloseButton="False"
ShowMaxRestoreButton="False"
ShowMinButton="False"
Height="768"
Width="1024">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<rx:ViewModelViewHost ViewModel="{Binding ActiveItem}" />
</Grid>
</mah:MetroWindow>
<UserControl x:Class="...NestedView"
Name="TheUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
TextOptions.TextHintingMode="Auto"
TextOptions.TextRenderingMode="Auto"
d:DesignHeight="768" d:DesignWidth="1024">
<Grid Background="DarkGray">
<Button Width="200" Height="100" />
</Grid>
</UserControl>
答案 0 :(得分:6)
这不是ReactiveUI问题,只是一个简单的WPF问题。
ViewModelViewHost
位于窗口的中心位置。虽然您的UserControl
设置了DesignHeight
和DesignWidth
,但在运行时呈现时,它会自动调整其内容的高度和宽度 - Button
。
将VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
添加到您的ViewModelViewHost
声明并删除其他两个Alignment
属性(默认为Stretch),并将其内容扩展为Window
的大小同时将任何具有固定大小的项目居中。