WPF - 工具提示:项目源不起作用



我在用户控件中有一个扩展器,并且我已经将工具提示设置为如下

 <Expander.Header >
            <Border Background="#cccccc"  Margin="2 8 8 8" Height="52"  >
                <Border.ToolTip>
                    <ToolTip >
                        <ToolTip.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="Foreground" Value="#cccccc"/>
                            </Style>
                        </ToolTip.Resources>
                        <Border Background="#4d4d4d" BorderThickness="0.5"  CornerRadius="5" MinWidth="200">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="200"/>
                                    <ColumnDefinition Width="180"/>
                                </Grid.ColumnDefinitions>
                                <!--#region Details -->
                                <WrapPanel Grid.Column="1"  Margin="8">
                                    ...
                                    <TextBlock Margin="4" Text="{Binding Description}"/>
                                     ...
                                 </WrapPanel>
  <!--#endregion--> 
  <!--#region Previews images-->
                                    <Border Grid.Column="0"   BorderBrush="Red" BorderThickness="0 0 0.5 0" MinHeight="200">
                                        <ItemsControl ItemTemplate="{StaticResource PanelCardToolTip}"  ItemsSource="{Binding DataContext.ListVoci,RelativeSource={RelativeSource AncestorType=Window}}"  MinWidth="120"  >
                                        <!--<ItemsControl ItemTemplate="{StaticResource PanelCardToolTip}"  ItemsSource="{Binding}"  MinWidth="120"  >-->
                                            <ItemsControl.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <WrapPanel />
                                                </ItemsPanelTemplate>
                                            </ItemsControl.ItemsPanel>
                                        </ItemsControl>
 </Border>
                                    <!--#endregion-->
                                </Grid>
                            </Border>
                        </ToolTip>
                    </Border.ToolTip>
 </Border>
            </Expander.Header>

数据模板如下:

   <DataTemplate x:Key="PanelCardToolTip">
        <Border Background="#4d4d4d"  BorderBrush="#cccccc" BorderThickness="0.5" Width="55">
            <StackPanel Margin="8" Orientation="Vertical">
                <TextBlock Text="Static Text" Foreground="White"></TextBlock>
                <Image Margin="2" Source="{Binding PathImgAnteprima}" Width="30"/>
                <TextBlock   FontWeight="Medium" Margin="2 4 2 2" Text="{Binding Etichetta}"/>
                <TextBlock   FontWeight="Light" Margin="2 0 2 2" Text="{Binding Descrizione}"/>
            </StackPanel>
        </Border>
        <!-- CardView -->
    </DataTemplate>

问题是 ItemControl 的 ItemsSource 不起作用(好像 ItemsSource 对 DataContext 不可见,但它是可见的)。具有相同 ItemsSource 和 DataTemplate 的相同 ItemsControl 在工具提示外部和扩展器内部的面板中工作正常。

非常感谢议员

您可以将 Border 元素的 Tag 属性绑定到窗口的 DataContext,然后绑定到工具提示的 PlacementTarget 的 Tag 属性:

<Expander.Header>
    <Border Background="#cccccc"  Margin="2 8 8 8" Height="52"
                        Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}">
        <Border.ToolTip>
            <ToolTip>
                <ToolTip.Resources>
                    <Style TargetType="TextBlock">
                        <Setter Property="Foreground" Value="#cccccc"/>
                    </Style>
                </ToolTip.Resources>
                <Border Background="#4d4d4d" BorderThickness="0.5"  CornerRadius="5" MinWidth="200">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="180"/>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0"   BorderBrush="Red" BorderThickness="0 0 0.5 0" MinHeight="200">
                            <ItemsControl ItemTemplate="{StaticResource PanelCardToolTip}"
                                          ItemsSource="{Binding PlacementTarget.Tag.ListVoci,RelativeSource={RelativeSource AncestorType=ToolTip}}" MinWidth="120">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <WrapPanel />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            </ItemsControl>
                        </Border>
                    </Grid>
                </Border>
            </ToolTip>
        </Border.ToolTip>
        <TextBlock>....</TextBlock>
    </Border>
</Expander.Header>

无法使用 RelativeSource 绑定绑定到工具提示中父窗口的属性的原因是工具提示驻留在其自己的可视化树中。

其他元素(如Text="{Binding **Description**}")可以正确访问并且工作正常。问题仅在未迭代列表的 ItemsSource 上。

工具提示上的<ToolTip DataContext="{Binding DataContext,在任一字段Text="{Binding **Description**}"都不起作用

  • 描述是 MyModel Class 的公共属性。
  • ListVoci是MyModel Class的一个ObservableCollection<Voci>
  • MyViewModel 是 MainWindow.xaml 和嵌入式用户控件的 DataContext。
  • ObservableCollection ListComm<MyModel>是列表视图的项源

就我而言,扩展器是包含在列表视图中使用的数据模板中的用户控件的一部分

代码写在下面

<ListView 
            ItemTemplate="{StaticResource PreviewCTemplate}"  
            ItemsSource="{Binding DataContext.ListComm, RelativeSource={RelativeSource AncestorType=Window}}"  
            SelectedItem="{Binding DataContext.SelectedComm, RelativeSource={RelativeSource AncestorType=Window}}"
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>

[已解决] - 真正的问题是'RelativeSource={RelativeSource AncestorType=Window}。最终解决方案如下

   <Border Grid.Column="1"   BorderBrush="#cccccc" BorderThickness="0.2 0 0 0" MinHeight="200">
   <ItemsControl  ItemTemplate="{StaticResource PanelCardToolTip}"  ItemsSource="{Binding ListVoci}"  MinWidth="120"  >
       <ItemsControl.ItemsPanel>
           <ItemsPanelTemplate>
               <WrapPanel />
           </ItemsPanelTemplate>
       </ItemsControl.ItemsPanel>
   </ItemsControl>

我所做的另一个操作是从 Expander.Header 的标签中写入 Itemscontrol

谢谢大家议员

最新更新