0

在使用 Converter 选择要用于我的控件的 DataTemplate 后,我无法维护绑定。

我的控件绑定到 IAppointment 的集合,其中一些是 WorkOrderAppointment 类型,一些是 DowntimeAppointment。我想根据类型定义一个工具提示。转换器正在根据类型选择正确的 DataTemplate,但 WorkOrderToolTipTemplate 和 DowntimeToolTipTemplate 中的绑定不起作用,就好像它们没有数据上下文一样。

有人可以帮助我了解发生了什么,甚至可能是解决方案吗?

缩写 XAML:

<UserControl.Resources>
    <DataTemplate x:Key="WorkOrderToolTipTemplate">
        <Grid
            Name="WorkOrderToolTips"
            Width="400"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="130*" />
                <ColumnDefinition Width="270*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <!-- Tooltip Labels -->
            <TextBlock
                Grid.Row="0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="Site Name:"
                />
            <TextBlock
                Grid.Row="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="Description:"
                />

            <!-- Tooltip Values -->
            <TextBlock
                Grid.Row="0"
                Grid.Column="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="{Binding Appointment.CalendarItem.SiteName}"
                TextWrapping="Wrap"
                />

            <TextBlock
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="{Binding Appointment.CalendarItem.Description}"
                TextWrapping="Wrap"
                />

        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="DowntimeToolTipTemplate">
        <Grid
            Name="DowntimeToolTips"
            Width="400"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="130*" />
                <ColumnDefinition Width="270*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <!-- Tooltip Labels -->
            <TextBlock
                Grid.Row="0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="Site Name:"
                />
            <TextBlock
                Grid.Row="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="Event Classification:"
                />

            <!-- Tooltip Values -->
            <TextBlock
                Grid.Row="0"
                Grid.Column="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="{Binding Appointment.Downtime.PlantName}"
                TextWrapping="Wrap"
                />
            <TextBlock
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Text="{Binding Appointment.Downtime.EventClassification}"
                TextWrapping="Wrap"
                />
        </Grid>
    </DataTemplate>

    <converters:ToolTipSelectorConverter x:Key="local_ToolTipSelectorConverter"
        DowntimeDataTemplate="{StaticResource DowntimeToolTipTemplate}" 
        WorkOrderDataTemplate="{StaticResource WorkOrderToolTipTemplate}" />

    <DataTemplate x:Key="AppointmentItemTemplate">
        <TextBlock
        Style="{Binding Appointment, Converter={StaticResource local_AppointmentTextStyleConverter}}"
        Text="{Binding Appointment.Subject}"
        TextTrimming="WordEllipsis"
        TextWrapping="Wrap"
        >
        <ToolTipService.ToolTip>
            <ToolTip ContentTemplate="{Binding Appointment, Converter={StaticResource local_ToolTipSelectorConverter}}" />
        </ToolTipService.ToolTip>
        </TextBlock>
    </DataTemplate>


<UserControl.Resources>
4

1 回答 1

0

这是 . 的标准问题ToolTip,它不是可视化或逻辑树的一部分,因此不继承DataContext.

标准解决方案是将 的绑定到DataContext的:ToolTipDataContextPlacementTarget

<ToolTip 
   ContentTemplate="{Binding Appointment, Converter={StaticResource local_ToolTipSelectorConverter}}"
   DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"
/>
于 2012-11-12T20:33:52.663 回答