将 DataGridRowHeader 按钮工具提示绑定到行对象属性

Binding DataGridRowHeader button tooltip to row object properties

我的 DataGridRowHeader 模板中有一个按钮,我将其放置在列的右侧。

这种布局的好处是,当您单击按钮时,它不会 select 行,就像您将它放在列中一样。

我现在发现的问题是我试图绑定行的对象属性,例如在按钮的 ToolTip 中。它一直返回空白,即使我对其进行了硬编码。Visibility 上的相同类型的绑定工作正常。

如何修复工具提示?

<DataGrid.RowHeaderTemplate>
    <DataTemplate>
        <Grid>
            <Button x:Name="btnSelectParent"
                    Template="{StaticResource SelectParentButtonStyle}"
                    Height="15" Width="15" 
                    Margin="0,0,-669,0" 
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    ToolTipService.ShowOnDisabled="True"
                    Visibility="{Binding DataContext.ShowSelectParentBtn, RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource bool2VisibilityConverter}}" >

                <Button.ToolTip>
                    <TextBlock TextAlignment="Left" 
                               Foreground="Black" 
                               FontWeight="Normal" 
                               Text="{Binding DataContext.ParentBtnMessage, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
                </Button.ToolTip>

            </Button>
        </Grid>
    </DataTemplate>
</DataGrid.RowHeaderTemplate>

DataGridRow 不是 RowHeaderTemplateButton 的视觉祖先,但 DataGridRowHeader 是:

Visibility="{Binding DataContext.ShowSelectParentBtn, RelativeSource={RelativeSource AncestorType=DataGridRowHeader}, Converter={StaticResource bool2VisibilityConverter}}"

你的第二个问题是 Tooltip 驻留在它自己的元素树中。您可以通过将 ButtonTag 属性 绑定到 DataGridRowHeaderDataContext 来解决这个问题,然后使用PlacementTargetTooltip:

<Button x:Name="btnSelectParent"
        ...
        Tag="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=DataGridRowHeader}}">
    <Button.ToolTip>
        <ToolTip>
            <TextBlock TextAlignment="Left" 
                       Foreground="Black" 
                       FontWeight="Normal" 
                       Text="{Binding PlacementTarget.Tag.ParentBtnMessage, RelativeSource={RelativeSource AncestorType=ToolTip}}"/>
        </ToolTip>
    </Button.ToolTip>
</Button>