简体   繁体   中英

What is the point of the StatusBarItem class in WPF?

How is the StatusBarItem class supposed to be used? Is every element in a StatusBar 's content supposed to be wrapped with it?

I don't really understand how StatusBarItem affects the StatusBar layout. It seems like I can use HorizontalAlignment on a StatusBarItem , but not when I put the element in the StatusBar directly:

<StatusBar>
    <TextBlock HorizontalAlignment="Right" Text="Not right" />
    <StatusBarItem HorizontalAlignment="Center">
        <TextBlock Text="Center" />
    </StatusBarItem>
</StatusBar>

Also if you wrap a Separator in a StatusBarItem the Separator changes to horizontal. Separator s default to vertical when put in the StatusBar directly without a StatusBarItem wrapper.

A StatusBar is an ItemsControl . All ItemsControl s have a container class. For ListBox es, it's ListBoxItem . For StatusBar , it's StatusBarItem . If you don't explicitly wrap your item in a StatusBarItem , it will be implicitly wrapped in one for you.

If you need to set properties of an ItemsControl 's containers, you can use the ItemContainerStyle property:

<StatusBar>
    <TextBlock>One</TextBlock>
    <TextBlock>Two</TextBlock>
    <TextBlock>Three</TextBlock>
    <StatusBar.ItemContainerStyle>
        <Style TargetType="StatusBarItem">
            <Setter Property="HorizontalAlignment" Value="Right"/>
        </Style>
    </StatusBar.ItemContainerStyle>
</StatusBar>

Finally, note that the StatusBar uses a DockPanel by default to lay out its children. This can be frustrating when you're doing intricate layouts. See my blog post here on how to swap it out for a Grid .

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