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>

在代码隐藏中定义了许多 DependencyProperties,它们绑定到 Image 的 ToolTip 属性中的各种控件。

我的问题是 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 从中获取绑定值。只要当前元素数据上下文为空,绑定引擎就会找到第一个非空父数据上下文,因此您可以像这样简单绑定

它会查找 UserControl 的可视层次结构并将其作为 DataContext。

于 2012-04-11T18:15:26.600 回答
0

我认为问题与 ToolTip 与它所属的控件不在同一个可视化树中有关。

I solved the problem by adding properties to the view model object for each control in the ToolTip template. As I needed to include a thumbnail image, and I'm storing images in my database as byte arrays, I wrote a class that implements IValueConverter that converts a byte array into a BitmapImage.

This all works. Thanks anyways.

于 2012-04-12T16:00:44.450 回答