【问题标题】:c# wpf binding not working with datacontextsc# wpf 绑定不使用数据上下文
【发布时间】:2017-03-12 16:03:01
【问题描述】:

我希望为列表框中的条目显示工具提示。 该工具包将包含一个文本框和列表框中条目中图像的副本(较大) 我可以获取文本框中的文本或要显示的图像。 显示图像但不显示文本的代码是

<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

文本按预期工作,但图像现在失败(如预期)

我已经尝试过 a) 修改原始文本块绑定指向驱动列表框条目的 FTitle 条目可观察集合

<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

都没有用。 (我确实尝试了很多变体,但没有一个奏效。

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

【问题讨论】:

    标签: c# wpf xaml data-binding listbox


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多