WPF TextBlock Overlaps Ellipse

霸气de小男生 提交于 2019-12-19 02:02:16

问题


I am trying to create this in WPF (I realize I could just use an image, but I am trying to learn 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>

回答1:


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>




回答2:


Or you can use the unicode character:

code 0x24D8

 <TextBlock Text="ⓘ" FontSize="52" />



回答3:


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>



回答4:


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.

To have a transparent background, you have to either set the TextBlock's Background property to Transparent, or set a null background.

Background={x:Null}


来源:https://stackoverflow.com/questions/17970797/wpf-textblock-overlaps-ellipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!