c#wpf绑定不适用于数据上下文
我希望在列表框中显示条目的工具提示。 工具箱将在列表框的条目中包含一个文本框和一个图像副本(较大) 我可以获取文本框中的文本或要显示的图像。 显示图像但不显示文本的代码为c#wpf绑定不适用于数据上下文,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,我希望在列表框中显示条目的工具提示。 工具箱将在列表框的条目中包含一个文本框和一个图像副本(较大) 我可以获取文本框中的文本或要显示的图像。 显示图像但不显示文本的代码为 <ControlTemplate TargetType="{x:Type ListBoxItem}"> <Border x:Name="Bd" SnapsToDevicePixels="true" Background="#EEFFFFFF" BorderBrush
<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>
此代码是列表框使用的代码的一部分
下面的代码(如上面列表中所示)将在工具提示中显示图像,但不会显示文本框
<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
DataContext=“{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}”来自{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}
将绑定(一个ListBoxItem)作为工具提示的DataContext。似乎您希望ListBoxItem的DataContext绑定到基础模型的属性。在这种情况下,请尝试将DataContext绑定更改为:{binding Path=PlacementTarget.DataContext,RelativeSource={x:Static RelativeSource.Self}
{Binding Path=PlacementTarget,RelativeSource={x:Static RelativeSource.Self}}
将(一个ListBoxItem)绑定为工具提示的DataContext。似乎您希望ListBoxItem的DataContext绑定到基础模型的属性。在这种情况下,请尝试将DataContext绑定更改为:{Binding Path=PlacementTarget.DataContext,RelativeSource={x:Static RelativeSource.Self}}
<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}}"/>
<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