简体   繁体   English

ContentControl不与TextBlock中的文本对齐

[英]ContentControl not aligning with text within a TextBlock

I have a Hyperlink with various bindings that I have put into a DataTemplate to ensure the code isn't repeated. 我有一个带有各种绑定的超链接,已将这些绑定放入DataTemplate中,以确保代码不会重复。 To use this I specify a ContentControl with a ContentTemplate. 为了使用它,我用ContentTemplate指定一个ContentControl。 However, using a ContentControl inline with TextBlock text causes the link to be offset. 但是,将ContentControl内联与TextBlock文本一起使用会导致链接偏移。 I've got down to the following test case where this occurs: 我已经确定了以下发生这种情况的测试用例:

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

在此处输入图片说明

The only way I've found to get around this is to specify a negative margin on the ContentControl, but obviously this is not ideal as it will not work when the font size changes. 我发现解决此问题的唯一方法是在ContentControl上指定负的页边距,但是显然这不是理想的,因为当字体大小更改时,它将不起作用。

I don't think it's possible to get a ContentControl to behave the same as an inline element, and the only way to get a ContentControl to stay inline with the text would be to modify padding/baseline etc. which can't respond to DPI changes and is obviously a bit of a hack. 我认为不可能让ContentControl的行为与内联元素相同,而让ContentControl保持与文本内联的唯一方法是修改填充/基线等,而该行为无法响应DPI。变化,显然有点hack。

I used Grx70's suggestion and instead moved my bindings into a style which I then set on the Hyperlink as needed: 我使用了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