multiple buttons in appbar

佐手、 提交于 2019-12-12 11:01:09

问题


I'm afraid I'm missing something simple here, but I'm trying to put multiple buttons in my app bar.

here's the code for the initial app bar

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left">
        <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
    </AppBar>
</common:LayoutAwarePage.TopAppBar>

Now this code works, but I want to add a second button.

I thought that this would be fairly simple to do, I attempted:

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left"> <!-- line 19 -->
        <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
        <Button  x:Name="newButton" Content="NewButton" />  <!-- line 21 -->
    </AppBar>
</common:LayoutAwarePage.TopAppBar>

but I get the following errors:

The property "Content" can only be set once. (line 19)
The property "Content" can only be set once. (line 21)
Duplication assignment to the 'Content' property of the 'AppBar' object (line 21)

I think there might perhaps be something to the AppBar control that I'm missing. How do i put a second button in it?


回答1:


Simply put everything in a StackPanel in AppBar

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left"> <!-- line 19 -->
        <StackPanel Orientation="Horizontal">
            <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
            <Button  x:Name="newButton" Content="NewButton" />  <!-- line 21 -->
        </StackPanel>
    </AppBar>
</common:LayoutAwarePage.TopAppBar>

For better control, you could use a Grid which will allow you to have content on both sides:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <StackPanel x:Name="LeftPanel" Orientation="Horizontal" Grid.Column="0" HorizontalAlignment="Left">
        <!-- Left Content here -->
    </StackPanel>
    <StackPanel x:Name="RightPanel" Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Right">
        <!-- Right Content here -->
    </StackPanel>
</Grid>



回答2:


You are setting content twice which is exactly what the error says. Instead, set the content to something that accepts multiple children like a `StackPanel', like so:

<common:LayoutAwarePage.TopAppBar>
    <AppBar HorizontalAlignment="Left"> <!-- line 19 -->
        <StackPanel Orientation="Horizontal">
            <Button  x:Name="resetButton" Content="Reset" HorizontalAlignment="Left" VerticalAlignment="Stretch" Click="resetButton_Click" />
            <Button  x:Name="newButton" Content="NewButton" />  <!-- line 21 -->
        </StackPanel>
    </AppBar>
</common:LayoutAwarePage.TopAppBar>


来源:https://stackoverflow.com/questions/13421533/multiple-buttons-in-appbar

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