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

How to bind to property of sibling control?

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

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

要么,要么有没有办法将图像绑定到父数据上下文中采用参数的方法?

谢谢。

J

没关系...我能够通过绑定的 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" />