C# 将ContentControl内容与窗口内容绑定
我有一扇没有标题栏的窗户C# 将ContentControl内容与窗口内容绑定,c#,wpf,mvvm,mvvm-light,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"
<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中加载了malty用户控件
在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();
SearchViewModel vm=newsearchviewmodel();
SearchView视图=新建SearchView();
view.DataContext=vm;
PopupWindow=新的PopupWindow();
window.Owner=Application.Current.Windows.OfType().SingleOrDefault(x=>x.IsActive);
window.Content=view;
ShowDialog();
为PopupWindow内容设置了Searchview,但我想为ContentControl的内容设置Searchview。如何设置此选项?不要在窗口上设置内容,而是直接在ContentControl上设置:
window.content.Content = view;
谢谢,但我在另一个类中设置了windows的内容,所以我无法访问内容控件。它也可以从其他类访问。您尝试过访问它吗?没有,我使用
是的,您需要x:FieldModifier=“public”
以防它们位于单独的程序集中。但若类位于同一程序集中,则可以直接访问它。