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

画像のToolTipプロパティにさまざまなコントロールにバインドされているコードビハインドで定義されて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') 

私は間違っていますか?バインディングを動作させるにはどうすればよいですか?あなたの相対的結合が評価され、指定された親を見つけることができない時はいつでも

トニー

答えて

0

私は、問題はツールヒントが、それが属しているコントロールと同じビジュアルツリーにないことと関係していると考えています。

ツールチップテンプレートの各コントロールのビューモデルオブジェクトにプロパティを追加することで、この問題を解決しました。私はサムネイルイメージを含める必要があり、イメージをデータベースにバイト配列として格納するため、バイト配列をBitmapImageに変換するIValueConverterを実装するクラスを作成しました。

これはすべて動作します。とにかく、ありがとう。

0

は、このエラーが表示されます。あなたはReadPushPinコントロールを見つけようとしていますが、それはビジュアルツリーに見つかりません。

コードを見る私はあなたのReadPushPinがここで指定されたUserControlだと思います。次に、相対ソースの祖先型をUserControlに設定する必要があります。それはトリックを行うだろう。

また、このコントロールでDataContextを設定しており、relativeSourceを使用してDataContextからバインドされた値を取得しようとしています。現在の要素のDataContextがnullでいつでもバインディングエンジンは、このようにあなたは、単純な

のように結合でき、最初の非ヌルの親のDataContextを発見し、それがユーザーコントロールに視覚的な階層構造をルックアップし、それはDataContextのだかかるだろう。

+0

私は、DataContextで見つかるものにRelativeSourceバインディングを使用していません。 NearestAddressプロパティとCrossStreetプロパティをDataContext内のオブジェクトに移動しました。これは私が今修正する必要があるThumbnailImage&HasThumbnailプロパティです。タイプをUserControlに変更してみて、どうなるか見てみましょう。 –

関連する問題