2012-04-11 184 views
0

我的应用程序使用WPF作为表示层。将控件属性绑定到工具提示中的控件

<UserControl x:Class="CarSystem.CustomControls.ReadPushPin" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:cs="clr-namespace:CarSystem.CustomControls" 
      mc:Ignorable="d" 
      DataContext="{Binding Path=Read, RelativeSource={RelativeSource Self}}" 
      d:DesignHeight="30" 
      d:DesignWidth="30"> 

    <UserControl.Resources> 
     <cs:BooleanToVisibilityConverter x:Key="BoolToVisibility" True="Visible" False="Collapsed" /> 
     <cs:DateConverterForRadDateTimePicker x:Key="DateConverter" /> 
    </UserControl.Resources> 

    <Image Name="MarkerImage" 
      Source="{Binding Path=Source, RelativeSource={RelativeSource AncestorType={x:Type cs:ReadPushPin}}}"> 
     <Image.ToolTip> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 
       <Image Grid.Column="0" 
         Grid.ColumnSpan="2" 
         Grid.Row="0" 
         Height="45" 
         HorizontalAlignment="Center" 
         Source="{Binding Path=ThumbnailImage, RelativeSource={RelativeSource AncestorType={x:Type cs:ReadPushPin}}}" 
         Visibility="{Binding Converter={StaticResource BoolToVisibility}, Path=HasThumbnail, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type cs:ReadPushPin}}}" 
         Width="60" /> 
       <TextBlock Grid.Column="0" 
          Grid.Row="1" 
          HorizontalAlignment="Right" 
          Text="Plate:" /> 
       <StackPanel Grid.Column="1" 
          Grid.Row="1" 
          HorizontalAlignment="Left" 
          Orientation="Horizontal"> 
        <TextBlock Text="{Binding Path=Plate}" /> 
        <TextBlock Text=", " /> 
        <TextBlock Text="{Binding Path=State}" /> 
       </StackPanel> 
       <TextBlock Grid.Column="0" 
          Grid.Row="2" 
          HorizontalAlignment="Right" 
          Text="Time:" /> 
       <TextBlock Grid.Column="1" 
          Grid.Row="2" 
          HorizontalAlignment="Left" 
          Text="{Binding Converter={StaticResource DateConverter}, Path=TimeStamp}" /> 
       <TextBlock Grid.Column="0" 
          Grid.Row="3" 
          HorizontalAlignment="Right" 
          Text="Nearest Address:" /> 
       <TextBlock Grid.Column="1" 
          Grid.Row="3" 
          HorizontalAlignment="Left" 
          Text="{Binding Path=NearestAddress, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type cs:ReadPushPin}}}" /> 
       <TextBlock Grid.Column="0" 
          Grid.Row="4" 
          HorizontalAlignment="Right" 
          Text="Cross Street:" /> 
       <TextBlock Grid.Column="1" 
          Grid.Row="4" 
          HorizontalAlignment="Left" 
          Text="{Binding Path=CrossStreet, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type cs:ReadPushPin}}}" /> 
      </Grid> 
     </Image.ToolTip> 
    </Image> 
</UserControl> 

有许多在绑定到不同的控制图像的工具提示属性的代码隐藏定义DependencyProperties的:我在我的代码,其XAML如下所示有一个UserControl

我的问题是ThumbnailImage,HasThumbnail,NearestAddress和CrossStreet属性上的绑定不起作用。在程序运行时,在调试输出窗口中看到如下错误:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='CarSystem.CustomControls.ReadPushPin', AncestorLevel='1''. BindingExpression:Path=CrossStreet; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

我在做什么错?我如何获得绑定的工作?

Tony

回答

0

我相信问题与工具提示不属于它所属的控件所在的视觉树中。

我通过向工具提示模板中每个控件的视图模型对象添加属性来解决该问题。因为我需要包含一个缩略图图像,并且我将图像作为字节数组存储在数据库中,所以我编写了一个实现IValueConverter的类,该类将字节数组转换为BitmapImage。

这一切正常。不管怎么说,多谢拉。

0

无论何时评估相对绑定并找不到指定的父级,都会出现此错误。您要求它找到ReadPushPin控件,并且它在视觉树中找不到。

看看代码我猜你的ReadPushPin是这里指定的UserControl。那么你应该将相对源祖先类型设置为UserControl。这会做到这一点。

另外,我想你正在这个控件上设置DataContext,并尝试使用relativeSource从它获得绑定值。绑定引擎找到的第一个非空父的datacontext每当当前元素的datacontext为空,所以你可以像

简单的约束力,它会查找的视觉层次的用户控件,并把它的DataContext的。

+0

我没有在DataContext中找到的任何东西上使用RelativeSource绑定。我将NearestAddress和CrossStreet属性移动到DataContext中的对象上,他们即将出现。这是我现在需要修复的ThumbnailImage&HasThumbnail属性。我会尝试将类型更改为UserControl并查看会发生什么。 – 2012-04-11 18:38:26

相关问题