WPF TextBlock 重叠椭圆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17970797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF TextBlock Overlaps Ellipse
提问by user1477388
I am trying to create this in WPF (I realize I could just use an image, but I am trying to learn WPF):
我正在尝试在 WPF 中创建它(我意识到我只能使用图像,但我正在尝试学习 WPF):
(source)
(来源)
This is what I have so far but it isn't producing the desired result, in that, the textbox seems completely hide the ellipse whereas it should simply have a transparent background:
这是我到目前为止所拥有的,但它没有产生预期的结果,因为文本框似乎完全隐藏了椭圆,而它应该只是具有透明背景:
<StackPanel>
<TextBlock HorizontalAlignment="Left" Margin="144,207,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
<Ellipse HorizontalAlignment="Left" Height="52" Margin="142,189,0,0" Stroke="Black" VerticalAlignment="Top" Width="52"/>
</StackPanel>
回答by Andy
You can put things like this in a viewbox to make scaling easier, something like this. You'll need to remove the stack panel, it's going to stack items one on top of the other which isn't what you're after here. I used a grid in this case.
你可以把这样的东西放在一个视图框中,使缩放更容易,就像这样。您需要移除堆栈面板,它会将项目一个叠放在另一个上面,这不是您在这里想要的。在这种情况下,我使用了网格。
<Viewbox Width="100" Height="100">
<Grid Width="20" Height="20">
<Ellipse Stroke="Black"/>
<TextBlock HorizontalAlignment="Center" Text="i" TextAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Viewbox>


回答by Henk Holterman
Or you can use the unicode character: ?
或者您可以使用 unicode 字符: ?
code 0x24D8
代码 0x24D8
<TextBlock Text="?" FontSize="52" />
回答by Cramja
So a stackpanel will place the first item at the top, the second just below it, third below the second, and so forth. What you could do is use a Canvas or a Grid. Like the stackpanel, they are "Content Controls" and support placing multiple objects inside of them like you have done with the stackpanel.
因此,堆栈面板会将第一个项目放在顶部,第二个放在它的正下方,第三个放在第二个下方,依此类推。您可以做的是使用画布或网格。与堆栈面板一样,它们是“内容控件”并支持在其中放置多个对象,就像您对堆栈面板所做的那样。
So a really quick way to do what you're trying to accomplish would be:
因此,完成您要完成的工作的一种非常快速的方法是:
<Grid >
<Ellipse HorizontalAlignment="Left" Height="52" Stroke="Black" VerticalAlignment="Top" Width="52"/>
<TextBlock Text="i" FontSize="52" Margin="18,-13,-6,13" />
</Grid>
回答by So Many Goblins
Don't use a StackPanel for this, the purpose of it is to stack things, not show them overlapped, you're using the wrong tool for that. Use a Grid, it's far more suited for what you're trying to do.
不要为此使用 StackPanel,它的目的是堆叠事物,而不是显示它们重叠,您为此使用了错误的工具。使用网格,它更适合您要做的事情。
To have a transparent background, you have to either set the TextBlock's Background propertyto Transparent, or set a null background.
要获得透明背景,您必须将 TextBlock 的Background 属性设置为 Transparent,或者设置一个空背景。
Background={x:Null}

