0

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

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

我现在发现的问题是我正在尝试绑定行的对象属性,例如在按钮的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>
4

1 回答 1

1

DataGridRow is not a visual ancestor of a Button in a RowHeaderTemplate but DataGridRowHeader is:

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

Your second issue is that the Tooltip resides in its own element tree. You could solve this by binding the Tag property of the Button to the DataContext of the DataGridRowHeader and then bind to this property using the PlacementTarget of the Tooltip:

<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>
于 2017-09-06T10:19:29.683 回答