【发布时间】:2016-02-01 22:43:25
【问题描述】:
我是第一次尝试使用 AppBar,但在理解如何在风格上做我希望 AppBar 做的事情时遇到了一些困难。
以下是我定义的一些数据模板,以便我可以在 AppBar 内的按钮上使用图标。我不喜欢股票AppBarButton,因为它将按钮和图标堆叠在一起。我无法看到让它们水平堆叠,所以我只使用了自定义按钮模板。
<DataTemplate x:Key="NewFolderIconTemplate">
<Path Data="M10,4L12,6H20A2,2 0 0,1 22,8V18A2,2 0 0,1 20,20H4C2.89,20 2,19.1 2,18V6C2,4.89 2.89,4 4,4H10M15,9V12H12V14H15V17H17V14H20V12H17V9H15Z"
Fill="Black"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Width="24"
Height="24"
Margin="0 0 10 0"/>
</DataTemplate>
<DataTemplate x:Key="RefreshIconTemplate">
<Path Data="M19,12H22.32L17.37,16.95L12.42,12H16.97C17,10.46 16.42,8.93 15.24,7.75C12.9,5.41 9.1,5.41 6.76,7.75C4.42,10.09 4.42,13.9 6.76,16.24C8.6,18.08 11.36,18.47 13.58,17.41L15.05,18.88C12,20.69 8,20.29 5.34,17.65C2.22,14.53 2.23,9.47 5.35,6.35C8.5,3.22 13.53,3.21 16.66,6.34C18.22,7.9 19,9.95 19,12Z"
Fill="Black"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Width="24"
Height="24"
Margin="0 0 10 0"/>
</DataTemplate>
<DataTemplate x:Key="SortingIconTemplate">
<Path Data="M9.25,5L12.5,1.75L15.75,5H9.25M15.75,19L12.5,22.25L9.25,19H15.75M8.89,14.3H6L5.28,17H2.91L6,7H9L12.13,17H9.67L8.89,14.3M6.33,12.68H8.56L7.93,10.56L7.67,9.59L7.42,8.63H7.39L7.17,9.6L6.93,10.58L6.33,12.68M13.05,17V15.74L17.8,8.97V8.91H13.5V7H20.73V8.34L16.09,15V15.08H20.8V17H13.05Z"
Fill="Black"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Width="24"
Height="24"
Margin="0 0 10 0"/>
</DataTemplate>
接下来是实际的 AppBar。我在垂直堆栈面板中放置了一系列按钮,并将堆栈面板放在应用栏内,以便获得垂直菜单效果。
<Page.TopAppBar>
<AppBar HorizontalAlignment="Right"
Background="Transparent">
<AppBar.Resources>
<Style TargetType="Button"
x:Key="AppBarButton">
<Setter Property="HorizontalAlignment"
Value="Stretch" />
<Setter Property="Background"
Value="{ThemeResource ApplicationPageBackgroundThemeBrush}" />
<Setter Property="HorizontalContentAlignment"
Value="Left" />
</Style>
</AppBar.Resources>
<StackPanel HorizontalAlignment="Right">
<Button Command="{Binding Path=RefreshDirectoryListCommand}"
Style="{StaticResource AppBarButton}">
<StackPanel Orientation="Horizontal">
<ContentControl ContentTemplate="{StaticResource RefreshIconTemplate}"
VerticalAlignment="Center"
Margin="0 0 10 0"/>
<TextBlock Text="Refresh" />
</StackPanel>
</Button>
<Button Command="{Binding Path=CreateNewFolderCommand}"
Style="{StaticResource AppBarButton}">
<StackPanel Orientation="Horizontal">
<ContentControl ContentTemplate="{StaticResource NewFolderIconTemplate}"
VerticalAlignment="Center"
Margin="0 0 10 0"/>
<TextBlock Text="New Folder" />
</StackPanel>
</Button>
<Button Command="{Binding Path=SortListCommand}"
Style="{StaticResource AppBarButton}">
<StackPanel Orientation="Horizontal">
<ContentControl ContentTemplate="{StaticResource SortingIconTemplate}"
VerticalAlignment="Center"
Margin="0 0 10 0"/>
<TextBlock Text="Sort" />
</StackPanel>
</Button>
</StackPanel>
</AppBar>
</Page.TopAppBar>
我正在尝试解决一些问题。
-
AppBar的内容会在 AppBar 旁边弹出,而不是在它下方,就像下拉菜单一样。有没有办法配置 AppPBar 以这种方式运行,还是我必须设置自定义控件模板的样式?
- 当我将鼠标悬停在它们上面时,会呈现边框。 UWP 应用似乎不再有触发器,那么更改鼠标悬停外观的最佳方法是什么?我仍然可以通过样式来完成,还是必须替换控件模板?
- 在我的 AppBar 中选择一个按钮会导致该按钮变为半透明状态,从而允许其下方的内容渗入。设置此样式的最佳方式是什么?我想保留模糊动画,并删除透明度:/
- AppBar 本身的背景设置为白色,但是当我将鼠标悬停在它上面时,背景是灰色的。我想一旦我知道如何解决类似的边框问题和按钮上的透明度问题,这个问题就可以解决。
这似乎更像是我不知道如何在缺少触发器的情况下正确设置样式的问题。其他人通常会做什么来实现这些影响,构建自定义控制模板?如果是这样,Microsoft 是否为原始模板提供 XAML,以便您不必 100% 从头开始?
【问题讨论】:
标签: xaml winrt-xaml win-universal-app uwp