2016-12-13 52 views
0

在項目的列表框中,可以將一個項目指定爲主項目。在模板中,我有一個綁定到參數化命令的按鈕(集合中的特定項目是傳遞給datacontext中的命令的參數),僅當項目當前不是主項目時纔可見。如果項目是主項目,我想顯示一個靜態圖像。由於我無法將圖像綁定到命令以綁定按鈕,我想我可以將圖像的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> 

要麼,或有將圖像綁定到,需要一個參數父的datacontext的方法呢?

謝謝。

Ĵ

回答

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