2012-04-11 179 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

相關問題