开发者

WPF: Mimicing TextBlock auto-spacing between content

Ive noticed that putting TextBlocks inside another TextBlock automatically gives them some spacing in between. I would like to use this behaviour for a custom ItemsControl. I want content in this ItemsControl to automatically get a certain amount of pixels between them, but only between and not after the last item or before the first.

(Edit)

<TextBlock>
  <TextBlock Text="A" />
  <TextBlock Text="B" />
  <TextBlock Text="C" />
</TextBlock>

givese: "A B C" and not "ABC"

<ItemsControl>
  <Button Content="A" />
  <Button C开发者_如何学Goontent="B" />
  <Button Content="C" />
</ItemsControl>

gives three tightly packed buttons, but I want a margin around button B, ie spacing between every button, or whatever is inside this ItemsControl. I don't want to manually have to set Margins.


I found a solution to this myself. The trick is to find out if an item in the itemscontrol is the first or the last and set margin in the style according to this. There is a attachedproperty in itemscontrol called AlternationIndex that can be used for this. If setting the AlternationCount to 9999 we get AlternationIndex to be the same as the accual index (up to 9999) and can then set margin on the first (AlternationIndex == 0) to some other value then the default margin. Problem solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜