c#wpf绑定无法使用datacontexts

时间:2016-10-30 09:02:21

标签: c# wpf xaml data-binding listbox

我希望在列表框中显示一个条目的工具提示。 Toolkit将包含一个Textbox和Listbox条目中Image的副本(更大) 我可以在文本框或图像中显示文本。 显示图像但不显示文本的代码是

<ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border  x:Name="Bd" SnapsToDevicePixels="true"  Background="#EEFFFFFF" BorderBrush="#FFCCCCCC"   
                            HorizontalAlignment="Center" VerticalAlignment="Center"
                            BorderThickness="1">
                        <Grid>
                            <StackPanel Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left">                                    
                                <Image x:Name="img" ToolTipService.Placement="Top"
                                       Source="{Binding Path=ImageUri}" Height="64" Stretch="Uniform" Width="64">
                                    <Image.RenderTransform>
                                        <TransformGroup>
                                            <ScaleTransform ScaleX="1" ScaleY="1" x:Name="scaleTrans"/>
                                        </TransformGroup>
                                    </Image.RenderTransform>
                                    <Image.ToolTip>
                                        <ToolTip BorderBrush="{x:Null}" Background="{x:Null}" Effect="{x:Null}"
                                                 DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" 
                                                 HasDropShadow="False">
                                            <Border Background="{x:Null}" VerticalAlignment="Center" Margin="0" Width="600" 
                                                    HorizontalAlignment="Center">
                                                <Grid Background="{x:Null}">
                                                    <StackPanel >
                                                    <TextBlock Margin="5" Padding="5" FontSize="14" FontWeight="Bold"
                                                               Text="{Binding Path=FTitle}"
                                                               Background="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}"/> 
                                                    <Border Margin="8,0,8,12.5"  VerticalAlignment="top">
                                                            <Image Source="{Binding Path=Source}"/>
                                                    </Border>
                                                    </StackPanel>
                                                </Grid>
                                            </Border> 
                                        </ToolTip>
                                    </Image.ToolTip>
                                </Image>
                            </StackPanel>
                        </Grid>
                    </Border>
                </ControlTemplate>

此代码是ListBox使用的代码的一部分 下面的代码(如上面的列表中显示工具提示中的图像,但不显示文本框

<ToolTip BorderBrush="{x:Null}" Background="{x:Null}" Effect="{x:Null}"
     DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" 
     HasDropShadow="False">
             <Border Background="{x:Null}" VerticalAlignment="Center"Margin="0" Width="600" 
                                                    HorizontalAlignment="Center">
                <Grid Background="{x:Null}">
                   <StackPanel >
                      <TextBlock Margin="5" Padding="5" FontSize="14" FontWeight="Bold"
                                                               Text="{Binding Path=FTitle}"
                                                               Background="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}"/> 
                     <Border Margin="8,0,8,12.5"  VerticalAlignment="top">
                          <Image Source="{Binding Path=Source}"/>
                     </Border>
                  </StackPanel>
                </Grid>
              </Border> 
           </ToolTip>

如果删除

DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" from <ToolTip

Text按预期工作,但Image现在失败(正如预期的那样)

我试过 a)修改原始文本,使TextBlock绑定指向FTitle条目observable Collection驱动列表框条目

<TextBlock Margin="5" Padding="5" FontSize="14" FontWeight="Bold"
        Text="{Binding Path=DataContext.FTitle, RelativeSource={RelativeSource FindAncestor,                                               AncestorType={x:Type UserControl}}}"
        Background="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}"/> 

b)将datacontext移动到Image

<Border Margin="8,0,8,12.5"  VerticalAlignment="top">
       <Image Source="{Binding Path=DataContext.Source, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Image}}}"/>
                                                    </Border>enter code here

都没有奏效。 (我确实尝试了许多变化但没有效果。

我将不胜感激任何解决方案

1 个答案:

答案 0 :(得分:0)

{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}UIElement(一个ListBoxItem)绑定为工具提示的DataContext。您似乎需要ListBoxItem的DataContext才能绑定到基础模型的属性。在这种情况下,请尝试将DataContext绑定更改为:{Binding Path=PlacementTarget.DataContext, RelativeSource={x:Static RelativeSource.Self}}