简体   繁体   中英

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. To use this I specify a ContentControl with a ContentTemplate. However, using a ContentControl inline with TextBlock text causes the link to be offset. 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.

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.

I used Grx70's suggestion and instead moved my bindings into a style which I then set on the Hyperlink as needed:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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