0

Why does the app:ucPathSelection control Visibility binding below work...

<UserControl.Resources>
    <loc:VM_ucPathSelect x:Key="PathSelectVM" />
</UserControl.Resources>

...

<Grid Name="SelectionGrid">
  <Grid.DataContext>
     <Binding Source= "{StaticResource PathSelectVM}" />
  </Grid.DataContext>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="288*" />
    <ColumnDefinition Width="1" />
    <ColumnDefinition Width="288*" />
    <ColumnDefinition Width="1" />
    <ColumnDefinition Width="288*" />
    <ColumnDefinition Width="1" />
    <ColumnDefinition Width="288*" />
   </Grid.ColumnDefinitions>
   <GridSplitter Grid.Row="0" Grid.Column="1"
                 BorderThickness="1" BorderBrush="Gray" IsTabStop="False" />
   <GridSplitter Grid.Row="0" Grid.Column="3" 
                 BorderThickness="1" BorderBrush="Gray" IsTabStop="False" />
   <GridSplitter Grid.Row="0" Grid.Column="5" 
                 BorderThickness="1" BorderBrush="Gray" IsTabStop="False" />

   <app:ucPathSelectColumn Grid.Row="0" Grid.Column="0" 
       x:Name="ucPathSelectColumn1"
       Visibility="{Binding Source={StaticResource PathSelectVM}, Path=ColumnVisible1}"                    
       DataContext="{Binding Path=VM1}" />

    <app:ucPathSelectColumn Grid.Row="0" Grid.Column="2"
       x:Name="ucPathSelectColumn2"
       Visibility="{Binding Source={StaticResource PathSelectVM},Path=ColumnVisible2}"             
       DataContext="{Binding Path=VM2}" />

    <app:ucPathSelectColumn Grid.Row="0" Grid.Column="4"
       x:Name="ucPathSelectColumn3"
       Visibility="{Binding Source={StaticResource PathSelectVM},Path=ColumnVisible3}"
       DataContext="{Binding Path=VM3}"  />

     <app:ucPathSelectColumn Grid.Row="0" Grid.Column="6"
       x:Name="ucPathSelectColumn4"
       Visibility="{Binding Source={StaticResource PathSelectVM},Path=ColumnVisible4}"     
       DataContext="{Binding Path=VM4}"  />
</Grid>

...while removing the Source attribute causes it to fail?

   <app:ucPathSelectColumn Grid.Row="0" Grid.Column="0" 
       x:Name="ucPathSelectColumn1"
       Visibility="{Binding ColumnVisible1}"                    
       DataContext="{Binding Path=VM1}" />

    <app:ucPathSelectColumn Grid.Row="0" Grid.Column="2"
       x:Name="ucPathSelectColumn2"
       Visibility="{Binding Path=ColumnVisible2}"             
       DataContext="{Binding Path=VM2}" />

    <app:ucPathSelectColumn Grid.Row="0" Grid.Column="4"
       x:Name="ucPathSelectColumn3"
       Visibility="{Binding Path=ColumnVisible3}"
       DataContext="{Binding Path=VM3}"  />

     <app:ucPathSelectColumn Grid.Row="0" Grid.Column="6"
       x:Name="ucPathSelectColumn4"
       Visibility="{Binding Path=ColumnVisible4}"     
       DataContext="{Binding Path=VM4}"  />

The Source seems redundant, and yet it fails without it. The DataContext of the Grid element is set above as this control's VM. The DataContext of each app:ucPathSelectColumn is delivered as a property of the same VM... and this works without the same qualification. Only the Visibility was failing(Debug message, could not find Property on object...and the reported object was of type of the intended VM (PathSelectVM).

One property of the same VM is being used to bind to Visibility of the ucPathSelectColumn control, the other is property is the DataContext of the ucPathSelectColumn control. (EDIT: no, it isn't; it is reporting the type of the VM set via the same scope DataContext assignment. The answer below is correct.)

4

1 回答 1

1
   ...Visibility="{Binding Path=ColumnVisible1}"                    
   DataContext="{Binding Path=VM1}"...

删除 DataContext 绑定并检查。我的猜测是这个绑定隐藏了网格的绑定。

于 2013-03-14T23:37:28.917 回答