1

我有一个测试应用程序,它包含两个Windows和一个UserControl.

我想Window使用相同的方法在每个中插入控件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>

ControlColor.xaml:

<UserControl x:Class="WpfApplication2.View.ControlColor"
         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" 
         xmlns:viewModel="clr-namespace:WpfApplication2.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">


<Grid Background="{Binding Path=BackgroundColor}">

</Grid>
</UserControl>

ControlColorViewModel.cs:

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;
    }
  }
}

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>

当按下按钮时,MainWindow 中的 ContentControl 中的背景变为橙色,我希望插入 Window2 中的 ContentControl 也一样。使用相同的数据上下文。

我怎样才能获得在 MainWindow 中使用的相同数据上下文?

提前致谢。

4

3 回答 3

1

There are a lot of ways. The easy is to have a common, shared model property (CommonModel) and send it to every ViewModels. Next step is to use EventAggregator, but to my mind it's too complicated for you..

于 2012-12-20T12:17:49.397 回答
0

您可以使用称为Dependency Injection的东西来让您的控件使用相同的 DataContext。

Basically, you need to take it out of the XAML and 'find/resolve' your shared ControlColorViewModel in the constructor for your controls.

于 2012-12-20T12:11:00.647 回答
0

I'm a little new to WPF (having more of a WinForms background), but if I understand correctly you could define your dataContext1 in the App file resources and then they both could reference it.

于 2012-12-20T12:17:16.117 回答