-1

我试图将 dataContext 绑定到网格(下面的 xaml)

<Grid  ....    DataContext="{Binding Path=NewFormViewModel}"  > ...</Grid>

这个绑定不起作用,我意识到 NewFormViewModel 的 getter 从未被调用过。

此时,网格是 xaml 文件的根元素。

然后我在网格内放置了一个画布并在画布上进行了绑定,例如:

<Grid  ....> 
  <Canvas DataContext="{Binding Path=NewFormViewModel}">
      ....
  </Canvas>
</Grid>

数据绑定有效。

接下来,我尝试将网格更改为画布并再次进行数据绑定,如下所示:

<Canvas....    DataContext="{Binding Path=NewFormViewModel}"  > ...</Canvas>

绑定再次停止工作。

最后,我选择了一个嵌套在画布内的网格:

   <Canvas....> 
      <Grid DataContext="{Binding Path=NewFormViewModel}">
          ....
      </Grid>
    </Canvas>

问题是为什么对 xaml 的根元素的绑定不起作用?

或者我是否应该根本不使用 Canvas/Grid 作为根元素并使用 Page/UserControl 之类的东西?

编辑

我的逻辑树看起来像这样:

         Window   <- Data binding to object o
           |
           *
          Frame     <-Data binding to obect o inherited
           |
           *
         Canvas/Grid <- Data binding to o.NewFormViewModel failed
           |
           *
         Canvas/Grid <- Data binding to o.NewFormViewModel Succeeds

编辑2:

损坏的xml:

<Canvas x:Class="WPFEditors.NewForm"
      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" 
      mc:Ignorable="d" Height="398" Width="377"
      DataContext="{Binding Path=NewFormViewModel}"
     >
    <Grid  >
    <Label DataContext="Form Type" Height="31" HorizontalAlignment="Left" Margin="12,22,0,0" VerticalAlignment="Top" Width="92" />
    <ComboBox Margin="148,22,6,347" ItemsSource="{Binding Path=FormTypes}" />
    <Label Content="Description" Height="31" HorizontalAlignment="Left" Margin="12,58,0,0"  VerticalAlignment="Top" Width="92" />
    <Label Content="{Binding Path=Heading}" ToolTip="This is pulled from the Enum defined for FormTypes" Margin="148,59,6,309" />   
    <Label Content="Version" Height="31" HorizontalAlignment="Left" Margin="12,95,0,0"  VerticalAlignment="Top" Width="92" />
    <TextBox Text="Bind this later" Margin="148,97,6,270" Height="31" />
    <Label Content="Approval Level" Height="31" HorizontalAlignment="Left" Margin="12,132,0,0" VerticalAlignment="Top" Width="92" />
    <TextBox Height="31" Margin="148,134,6,233" Text="Bind this later" />
    <Label Content="Number of Approvals" Height="31" HorizontalAlignment="Left" Margin="12,171,0,0" VerticalAlignment="Top" Width="130" />
    <TextBox Height="31" Margin="148,173,6,194" Text="Bind this later" />
    <Label Content="Heading" Height="31" HorizontalAlignment="Left" Margin="12,210,0,0" VerticalAlignment="Top" Width="130" />
    <TextBox Height="31" Margin="148,212,6,155" Text="Bind this later" />
    <Label Content="Static Data Id" Height="31" HorizontalAlignment="Left" Margin="12,247,0,0" VerticalAlignment="Top" Width="130" />
    <TextBox Height="31" Margin="148,249,6,118" Text="Bind this later" />
    <Label Content="{Binding  Path=Errors}" Background="{Binding Path=Color}" Margin="12,325,6,6" BorderThickness="1" BorderBrush="#FF000019" />
    <Button Content="Create" Margin="83,294,202,78" />
    <Button Content="Create" Margin="181,294,104,78" />
    <Button Content="Create" Margin="279,294,6,78" />
    </Grid>
</Canvas>

在上面的 xaml 中,以下行绑定:

<Label Content="{Binding Path=Heading}" 

即使 Heading 位于该文档继承的视图模型的属性中。在这一点上,我会期待这条线:

 DataContext="{Binding Path=NewFormViewModel}"

将数据上下文更改为没有标题的 NewFormViewModel。所有剩余的绑定都失败。

如果我将开头更改为:

<Canvas x:Class="NewForm"
      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" 
      mc:Ignorable="d" Height="398" Width="377"

     >
    <Grid DataContext="{Binding Path=NewFormViewModel}" >

与 Heading 的绑定失败,其余绑定开始工作。这是我最初期望的行为。

此 xaml 嵌套在此文件中:

<Window x:Class="WPFEditors.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{Binding Path=Heading}" Height="743" Width="1177">

    <StackPanel  >
        <Menu IsMainMenu="True" >
            <MenuItem Header="New">
                <MenuItem Header="New Form" Command="{Binding Path=MenuCommand}" />
            </MenuItem>    
            <MenuItem Header="Edit Form"  ItemsSource="{Binding Path=FormsAvailable}" />
            <MenuItem Header="Edit Rules" />            
        </Menu>

        <Frame  NavigationUIVisibility="Hidden" Source="{Binding Path=CurrentPage}"
                LoadCompleted="Frame_LoadCompleted"
                DataContextChanged="Frame_DataContextChanged"
                Name="frame">
        </Frame>
    </StackPanel>

</Window>

将数据上下文复制到子级的事件处理程序是:

private void Frame_LoadCompleted(object sender, NavigationEventArgs e)
{
    UpdateFrameDataContext(); 
}

private void Frame_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    UpdateFrameDataContext(); 
}

private void UpdateFrameDataContext()
{
    var content = frame.Content as FrameworkElement; 
    if (content == null) 
        return; 
    content.DataContext = frame.DataContext;  
}
4

1 回答 1

0

如果控件层次结构(如 Window 或 UserControl)中存在(在其 DataContext 中)拥有您正在查询的属性的对象(NewFormViewModel),则根元素中的绑定应该起作用。

于 2012-05-14T18:46:05.460 回答