【问题标题】:How to bind to property of sibling control?如何绑定到兄弟控件的属性?
【发布时间】:2016-12-13 19:34:43
【问题描述】:

在项目列表框中,可以将一个项目指定为主项目。在模板中,我有一个绑定到参数化命令的按钮(集合中的特定项目是传递给数据上下文中命令的参数),仅当项目当前不是主要项目时才可见。如果该项目是主要项目,我想显示一个静态图像。由于我无法将图像绑定到我正在绑定按钮的命令,我想我可以将图像的 Visibility 属性绑定到按钮的 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>

要么这样,要么有办法将图像绑定到父数据上下文中带有参数的方法?

谢谢。

J

【问题讨论】:

    标签: wpf data-binding wpfdatagrid


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多