0

私のアプリケーションは、プレゼンテーション層に WPF を使用しています。コードにがUserControlあり、その XAML を以下に示します。

<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>

Image の ToolTip プロパティのさまざまなコントロールにバインドされているコード ビハインドで定義されている DependencyProperties が多数あります。

私の問題は、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')

私は何を間違っていますか?バインディングを機能させるにはどうすればよいですか?

トニー

4

2 に答える 2

0

このエラーは、相対バインディングが評価され、指定された親が見つからない場合に表示されます。ReadPushPinコントロールを見つけるように要求しましたが、ビジュアルツリーに見つかりません。

コードを見ると、ReadPushPinはここで指定されたUserControlであると思います。次に、相対ソースの祖先タイプをUserControlに設定する必要があります。それでうまくいくでしょう。

また、このコントロールにDataContextを設定していて、relativeSourceを使用してそこからバインドされた値を取得しようとしていると思います。バインディングエンジンは、現在の要素のデータコンテキストがnullの場合は常に、最初のnull以外の親データコンテキストを検出します。したがって、次のような単純なバインディングが可能です。

そして、視覚的な階層をUserControlまで検索し、そのDataContextを取得します。

于 2012-04-11T18:15:26.600 に答える
0

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

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

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

于 2012-04-12T16:00:44.450 に答える