此示例中是否需要冗余绑定源?没有它它就行不通

时间:2013-03-14 23:28:01

标签: wpf data-binding

为什么应用程序:ucPathSelection控制下面的可见性绑定工作...

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

...删除Source属性会导致它失败吗?

   <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}"  />

Source似乎是多余的,但如果没有它,它就会失败。 Grid元素的DataContext在上面设置为此控件的VM。每个应用程序的DataContext:ucPathSelectColumn作为同一个VM的属性提供......并且这个工作没有相同的限定条件。只有可见性失败(调试消息,无法在对象上找到属性...并且报告的对象是预期VM的类型(PathSelectVM)。

同一个VM的一个属性用于绑定到ucPathSelectColumn控件的Visibility,另一个属性是ucPathSelectColumn控件的DataContext。 (编辑:不,它不是;它通过相同的范围DataContext分配报告VM类型。下面的答案是正确的。)

1 个答案:

答案 0 :(得分:1)

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

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