ContentControl 未与 TextBlock 中的文本对齐
ContentControl not aligning with text within a TextBlock
我有一个带有各种绑定的 Hyperlink,我已将这些绑定放入 DataTemplate 中以确保代码不会重复。为了使用它,我指定了一个带有 ContentTemplate 的 ContentControl。但是,使用与 TextBlock 文本内联的 ContentControl 会导致 link 发生偏移。我已经了解了以下发生这种情况的测试用例:
<TextBlock>Text with a <ContentControl>Inline content control</ContentControl> in it.</TextBlock>
我发现解决这个问题的唯一方法是在 ContentControl 上指定一个负边距,但显然这并不理想,因为当字体大小改变时它不起作用。
我认为不可能让 ContentControl
的行为与内联元素相同,而让 ContentControl
与文本保持内联的唯一方法是修改 padding/baseline 等无法响应 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>
我有一个带有各种绑定的 Hyperlink,我已将这些绑定放入 DataTemplate 中以确保代码不会重复。为了使用它,我指定了一个带有 ContentTemplate 的 ContentControl。但是,使用与 TextBlock 文本内联的 ContentControl 会导致 link 发生偏移。我已经了解了以下发生这种情况的测试用例:
<TextBlock>Text with a <ContentControl>Inline content control</ContentControl> in it.</TextBlock>
我发现解决这个问题的唯一方法是在 ContentControl 上指定一个负边距,但显然这并不理想,因为当字体大小改变时它不起作用。
我认为不可能让 ContentControl
的行为与内联元素相同,而让 ContentControl
与文本保持内联的唯一方法是修改 padding/baseline 等无法响应 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>