繁体   English   中英

ContentControl不与TextBlock中的文本对齐

[英]ContentControl not aligning with text within a TextBlock

我有一个带有各种绑定的超链接,已将这些绑定放入DataTemplate中,以确保代码不会重复。 为了使用它,我用ContentTemplate指定一个ContentControl。 但是,将ContentControl内联与TextBlock文本一起使用会导致链接偏移。 我已经确定了以下发生这种情况的测试用例:

<TextBlock>Text with a <ContentControl>Inline content control</ContentControl> in it.</TextBlock>

在此处输入图片说明

我发现解决此问题的唯一方法是在ContentControl上指定负的页边距,但是显然这不是理想的,因为当字体大小更改时,它将不起作用。

我认为不可能让ContentControl的行为与内联元素相同,而让ContentControl保持与文本内联的唯一方法是修改填充/基线等,而该行为无法响应DPI。变化,显然有点hack。

我使用了Grx70的建议,而是将绑定移动到一种样式中,然后根据需要在Hyperlink上进行设置:

<Style x:Key="CustomHyperlink" TargetType="{x:Type Hyperlink}" BasedOn="{StaticResource {x:Type Hyperlink}}">
        <Setter Property="NavigateUri" Value="{Binding TheUri}"/>
        <Setter Property="Command" Value="{Binding TheCommand}"/>
        <Setter Property="CommandParameter" Value="{Binding NavigateUri, RelativeSource={RelativeSource Self}}"/>
</Style>

<TextBlock TextWrapping="Wrap">
        ...please <Hyperlink Style="{StaticResource CaseHyperlink}">view in browser</Hyperlink> and...
</TextBlock>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM