【问题标题】:How to set DataContext on multiple Windows/Pages/Controls?如何在多个 Windows/Pages/Controls 上设置 DataContext?
【发布时间】:2013-10-12 14:52:21
【问题描述】:

我有一个关于 WPF MVVM 工作原理和工作代码的问题,但不确定它为什么工作。大多数在线教程似乎都使用单个窗口提供示例,所以我不确定我是否使用多个窗口/页面/用户控件正确执行此操作。

如果我有一个名为ViewModel 的类,并且我使用下面的代码在MainWindow 中设置了DataContext,那么我只设置了MainWindowDataContext

MainWindow.xaml.cs:

public partial class MainWindow

{
    Private ViewModel viewModel = new ViewModel();

    public MainWindow()
    {
       InitializeComponent();
       this.DataContext = this.viewModel;
    }

}

如果我然后创建一个新的用户控件,然后绑定一个 DataGrid 而不指定视图模型的路径,为什么在我没有设置用户控件的 DataContext 时下面的代码可以工作?

这是 WPF 的工作方式还是我也应该在用户控件中设置 DataContext?这样做的正确方法是什么?

MainSignals.xaml:

<UserControl x:Class="ProjectXYZ.Content.MainSignals"
             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:core="clr-namespace:System;assembly=mscorlib"
             xmlns:local="clr-namespace:ProjectXYZ.Content"
             xmlns:mui="http://firstfloorsoftware.com/ModernUI"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" >
    <Grid>
        <DockPanel>
            <DataGrid Name="DG1" ItemsSource="{Binding ReceivedSignals}"  >
                <DataGrid.Columns>
                    <mui:DataGridTextColumn Header="SignalID"  Binding="{Binding signalID}"/>
            <mui:DataGridTextColumn Header="SignalType"  Binding="{Binding signalType}"/>
                </DataGrid.Columns>
            </DataGrid>
        </DockPanel>
    </Grid>
</UserControl>

ViewModel.cs:

private ObservableCollection<MainWindow.SignalVar> _receivedSignals;

Public ViewModel()
{
}

public event PropertyChangedEventHandler PropertyChanged;


// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

public ObservableCollection<MainWindow.SignalVar> ReceivedSignals
{
    get { return _receivedSignals; }
    set
    {
        if (value != _receivedSignals)
        {
            _receivedSignals = value;
            OnPropertyChanged("ReceivedSignals");
        }
    }
}

UserControl.xaml.cs:

public partial class MainSignals : UserControl
{

    public MainSignals()
    {
        InitializeComponent();
        //this.DataContext = new ViewModel();  //WORKS WITHOUT THIS??
    }

}

【问题讨论】:

    标签: c# wpf binding datacontext


    【解决方案1】:

    这是因为如果没有明确设置子控件的DataContext,子控件会继承父控件的DataContext。这对于某些DependancyProperties 是正确的,例如,如果您设置父控件的 Foreground,则所有子控件都继承相同的 Foreground 属性值。

    在您的情况下,由于您没有为子 UserControl 显式设置 DataContext,它将采用其父级的 DataContext,即您的 Window。

    【讨论】:

      猜你喜欢
      • 2018-04-15
      • 2012-11-09
      • 2012-09-07
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 2010-11-05
      相关资源
      最近更新 更多