C# 如何在另一个xaml中使用datacontext
我有一个测试应用程序,它由两个C# 如何在另一个xaml中使用datacontext,c#,wpf,user-controls,datacontext,C#,Wpf,User Controls,Datacontext,我有一个测试应用程序,它由两个Windows和一个UserControl组成 我想使用相同的DataContext在每个窗口中插入控件: MainWindow.xaml: <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winf
Windows
和一个UserControl
组成
我想使用相同的DataContext
在每个窗口中插入控件:
MainWindow.xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:WpfApplication2.View"
xmlns:viewModel="clr-namespace:WpfApplication2.ViewModel"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<viewModel:ControlColorViewModel x:Name="dataContext1"/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Width="64" Height="64" Command="{Binding
Path=PressedButton}">Press</Button>
<view:ControlColor Grid.Column="1" />
</Grid>
</Window>
Window2.xaml:
<Window x:Class="WpfApplication2.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:WpfApplication2.View"
Title="Window2" Height="300" Width="300">
<Grid>
<view:ControlColor />
</Grid>
</Window>
当按下按钮时,主窗口中ContentControl中的背景将变为橙色,我希望在Window2中插入的ContentControl也一样。使用相同的数据上下文
如何获得在MainWindow中使用的相同datacontext
提前感谢。您可以使用调用的东西使控件使用相同的DataContext
基本上,您需要将其从XAML中取出,并在控件的构造函数中“查找/解析”您的共享ControlColorViewModel。您可以使用调用的内容使控件使用相同的DataContext
基本上,您需要将其从XAML中取出,并在控件的构造函数中“查找/解析”您的共享ControlColorViewModel。我对WPF有点陌生(有更多WinForms背景),但是如果我理解正确,您可以在应用程序文件资源中定义dataContext1,然后他们都可以引用它。我对WPF有点陌生(有更多WinForms背景),但是如果我理解正确,您可以在应用程序文件资源中定义dataContext1,然后他们都可以引用它。有很多方法。最简单的方法是拥有一个公共的共享模型属性(CommonModel),并将其发送到每个ViewModels。
下一步是使用EventAggregator,但我认为这对您来说太复杂了。有很多方法。最简单的方法是拥有一个公共的共享模型属性(CommonModel),并将其发送到每个ViewModels。
下一步是使用EventAggregator,但在我看来,这对您来说太复杂了。这会起作用,但根据他的特殊需要,这也可能像是告诉他去买一个脱蜡钻,而他只需要一把简单的螺丝刀:)当然,在我的类比中,电钻必须是免费的,在这种情况下,谁不想去买一个免费的电钻呢?:)谢谢,目前我不认为我需要使用依赖注入,但这是我迟早会尝试的事情,就像EventAggregator一样,但根据他的特殊需要,这也可能像告诉他去买一个脱蜡钻,而他只需要一把简单的螺丝刀:)当然,在我的类比中,电钻必须是免费的,在这种情况下,谁不想去买一个免费的电钻呢?:)谢谢,目前我认为我不需要使用依赖注入,但我迟早会尝试使用它,就像我们在测试应用程序中尝试过这个解决方案一样。它是有效的,我想我会在项目中使用它:D.谢谢我已经在测试应用程序中尝试过这个解决方案。它是有效的,我想我会在项目中使用它:D.ThanksThanks,我会尽快查看EventAggregator,我会尽快查看EventAggregator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Input;
namespace WpfApplication2.ViewModel
{
class ControlColorViewModel : ViewModelBase
{
private Brush backgroundColor;
public Brush BackgroundColor
{
get { return this.backgroundColor; }
set
{
if (this.backgroundColor != value)
{
this.backgroundColor = value;
OnPropertyChanged("BackgroundColor");
}
}
}
public ICommand PressedButton { get { return new RelayCommand(param =>
this.SetPressedButton()); } }
public ControlColorViewModel()
{
}
private void SetPressedButton()
{
BackgroundColor = Brushes.Orange;
}
}
}
<Window x:Class="WpfApplication2.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:view="clr-namespace:WpfApplication2.View"
Title="Window2" Height="300" Width="300">
<Grid>
<view:ControlColor />
</Grid>
</Window>