使用Converter选择将哪个DataTemplate用于我的控件后,我无法维护绑定。
我的控件绑定到IAppointment的集合,有些是类型为WorkOrderAppointment的DowntimeAppointment。我想根据类型定义工具提示。转换器根据类型选择正确的DataTemplate,但WorkOrderToolTipTemplate和DowntimeToolTipTemplate中的绑定不起作用,就好像它们没有datacontext一样。
有人可以帮助我了解最新情况,甚至是解决方案吗?
缩写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>
答案 0 :(得分:0)
这是ToolTip
的标准问题,它不是视觉或逻辑树的一部分,因此不会继承DataContext
。
标准解决方案是将DataContext
的{{1}}绑定到ToolTip
的{{1}}:
DataContext