如何绑定兄弟控件的属性?

时间:2016-12-13 19:34:43

标签: wpf data-binding wpfdatagrid

在项目列表框中,可以将一个项目指定为主项目。在模板中,我有一个绑定到参数化命令的按钮(集合中的特定项是传递给datacontext中的命令的参数),仅当项目当前不是主项目时才可见。如果项目是主要项目,我想显示静态图像。由于我无法将图像绑定到命令上,因此我绑定了按钮,我想我可以将图像的Visibility属性绑定到" inverse"按钮的Visibility属性。 (即当按钮可见时,图像被隐藏,反之亦然。)但我无法弄清楚如何做到这一点。该按钮是模板内网格内的图像的兄弟。这是我的模板......

<DataTemplate>
  <StackPanel Orientation="Vertical">
    <Grid>
      <Grid.ColumnDefinitions>
      ...
      </Grid.ColumnDefinitions>

      <TextBlock Grid.Column="0" Text="{Binding Path=FormattedNumber}" Style="{StaticResource FieldDataTextBlock}" FontWeight="Bold" />
      <!-- How can I make this image aware of the following button's state? -->
      <Image Grid.Column="2" Source="/Resources/Star.Pressed.png" Visibility="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path={}}" Width="20" Height="20" />
      <Button Grid.Column="2" x:Name="btnMakePrimary" Style="{StaticResource StarButton}" Command="{Binding ElementName=lstPhoneNumbers, Path=DataContext.MakePrimaryPhoneNumberCommand}" CommandParameter="{Binding}" ToolTip="Set as display number." Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BoolVisibility}}" />
      <Button Grid.Column="4" Style="{StaticResource DetailsButton}" Command="{Binding ElementName=lstPhoneNumbers, Path=DataContext.ViewPhoneNumberCommand}" CommandParameter="{Binding}" />
      <Button Grid.Column="6" Style="{StaticResource DeleteButton}" Command="{Binding ElementName=lstPhoneNumbers, Path=DataContext.DeletePhoneNumberCommand}" CommandParameter="{Binding}" />
    </Grid>
    <Grid >
      <Grid.ColumnDefinitions>
      ...
      </Grid.ColumnDefinitions>

      <TextBlock Grid.Column="0" Text="{Binding Path=PhoneTypeString}" Style="{StaticResource FieldDataTextBlock}" />
      <TextBlock Grid.Column="2" Text="(notes)" Foreground="Blue" ToolTip="{Binding Path=PhoneNumberNote}" Visibility="{Binding Path=HasNote, Converter={StaticResource BoolVisibility}}" />
    </Grid>
  </StackPanel>
</DataTemplate>

是否有办法将图像绑定到带有参数的父datacontext中的方法?

感谢。

Ĵ

1 个答案:

答案 0 :(得分:0)

没关系......我能够通过绑定的ElementName属性实现它,并修复了包含的图像资源的问题,这会影响我的视觉效果:

<Button Grid.Column="2" Name="btnMakePrimary" Style="{StaticResource StarButton}" Command="{Binding ElementName=lstPhoneNumbers, Path=DataContext.MakePrimaryPhoneNumberCommand}" CommandParameter="{Binding}" ToolTip="Set as display number." Visibility="{Binding Path=IsEnabled, RelativeSource={RelativeSource Self}, Converter={StaticResource BoolVisibility}}" />
<Image Grid.Column="2" Source="/Resources/Star.Pressed.png" Visibility="{Binding ElementName=btnMakePrimary, Path=IsEnabled, Converter={StaticResource BoolVisibilityReverse}}" Width="20" Height="20" />