将ContentControl内容与窗口内容绑定

时间:2014-09-07 09:27:19

标签: c# wpf mvvm mvvm-light

我有一个没有标题栏的窗口。

<Window x:Class="WpfApplication1.PopupWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        WindowStyle="None" ResizeMode="NoResize"
        AllowsTransparency="True" Background="Transparent">
    <Border Margin="10">
        <Border.Effect>
            <DropShadowEffect Color="Black"
                              Direction="270"
                              BlurRadius="10"
                              ShadowDepth="3" />
        </Border.Effect>
        <Grid Background="White">
            <DockPanel>
                <DockPanel Name="titleBar"
                           DockPanel.Dock="Top"
                           Height="32"
                           Background="LimeGreen">
                    <TextBlock Padding="8"
                               VerticalAlignment="Center"
                               Text="My Special Window"
                               Foreground="White"
                               FontWeight="999"
                               FontSize="16" />
                </DockPanel>
                <ContentControl Name="content" />
            </DockPanel>
        </Grid>
    </Border>
</Window>

我有在PopupWindow中加载的恶意Usercontrol。

在viewmodel中,我用不同的内容显示这个窗口。

SearchViewModel vm = new SearchViewModel();
SearchView view = new SearchView();
view.DataContext = vm;
PopupWindow window = new PopupWindow();
window.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
window.Content = view;
window.ShowDialog();

为PopupWindow内容设置Searchview,但我想为ContentControl的内容设置searchview。我怎么这样?

1 个答案:

答案 0 :(得分:2)

不是在Window上设置内容,而是直接在ContentControl上设置:

window.content.Content = view;