MasterDetailsView 中的 CommandBar 放置
CommandBar placement inside MasterDetailsView
我使用 Windows Template Studio 为 Windows 10 创建了一个项目,并添加了一个主从页面。然后,我将 master 和 details 命令栏添加到 Microsoft.Toolkit.Uwp.UI.Controls.MasterDetailsView 中,如下所示:
<Page
x:Class="MasterDetails.Views.MasterDetailPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="{StaticResource PageStyle}"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:model="using:MasterDetails.Models"
xmlns:views="using:MasterDetails.Views"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Key="ItemTemplate" x:DataType="model:SampleOrder">
<Grid Height="64" Padding="0,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<FontIcon Grid.Column="0" Tag="{x:Bind HashIdentIcon}" FontSize="40" Glyph="{x:Bind SymbolAsChar}" VerticalAlignment="Center" />
<StackPanel Grid.Column="1" Margin="12,0,0,0" VerticalAlignment="Center">
<TextBlock Text="{x:Bind Company}" Tag="{x:Bind HashIdentTitle}" Style="{ThemeResource ListTitleStyle}"/>
<TextBlock Text="{x:Bind Status}" Style="{StaticResource ListSubTitleStyle}"/>
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DetailsTemplate">
<views:MasterDetailDetailControl MasterMenuItem="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="NoSelectionContentTemplate">
<TextBlock x:Uid="MasterDetail_NoSelection" Style="{StaticResource ListNoSelectionTextStyle}" />
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition x:Name="TitleRow" Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock
x:Uid="MasterDetail_Title"
x:Name="TitlePage"
Margin="12,0,12,7"
Style="{StaticResource PageTitleStyle}" />
<controls:MasterDetailsView
Grid.Row="1"
x:Name="MasterDetailsViewControl"
ItemsSource="{x:Bind SampleItems}"
SelectedItem="{x:Bind Selected, Mode=OneWay}"
ItemTemplate="{StaticResource ItemTemplate}"
DetailsTemplate="{StaticResource DetailsTemplate}"
NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"
BorderBrush="Transparent">
<controls:MasterDetailsView.MasterCommandBar>
<CommandBar HorizontalAlignment="Left">
<AppBarButton Icon="Refresh" Label="Refresh"/>
<AppBarButton Icon="Add" Label="Add"/>
</CommandBar>
</controls:MasterDetailsView.MasterCommandBar>
<controls:MasterDetailsView.DetailsCommandBar>
<CommandBar HorizontalAlignment="Left">
<AppBarButton Icon="Edit" Label="Edit"/>
</CommandBar>
</controls:MasterDetailsView.DetailsCommandBar>
</controls:MasterDetailsView>
</Grid>
</Page>
命令栏位于底部(分别位于主面板和详细面板的底部)。我想让他们在顶部。我怎样才能做到这一点?
我尝试将主命令栏放在 MasterDetailsView 外面,将详细信息命令栏放在滚动查看器之前 MasterDetailDetailControl.xaml,但这看起来不太好,而且占用了不必要的空间 space。
您需要 re-template MasterDetailsView
.
- 您可以从 MasterDetailsView.xaml
获取默认模板
更改 <Grid.RowDefinitions>
中的行顺序
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
将 MasterList
的行号更改为 Grid.Row="2"
,将 MasterCommandBarPanel
的行号更改为 Grid.Row="1"
<Grid x:Name="MasterPanel"
Width="{TemplateBinding MasterPaneWidth}"
Background="{TemplateBinding MasterPaneBackground}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="0,0,1,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentPresenter x:Name="HeaderContentPresenter"
Margin="12,0"
x:DeferLoadStrategy="Lazy"
Content="{TemplateBinding MasterHeader}"
ContentTemplate="{TemplateBinding MasterHeaderTemplate}"
Visibility="Collapsed" />
<Grid x:Name="MasterCommandBarPanel" Grid.Row="1"></Grid>
<ListView x:Name="MasterList"
Grid.Row="2"
IsTabStop="False"
ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
ItemTemplate="{TemplateBinding ItemTemplate}"
ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
ItemsSource="{TemplateBinding ItemsSource}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
对DetailsPanel
做同样的事情
我使用 Windows Template Studio 为 Windows 10 创建了一个项目,并添加了一个主从页面。然后,我将 master 和 details 命令栏添加到 Microsoft.Toolkit.Uwp.UI.Controls.MasterDetailsView 中,如下所示:
<Page
x:Class="MasterDetails.Views.MasterDetailPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="{StaticResource PageStyle}"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:model="using:MasterDetails.Models"
xmlns:views="using:MasterDetails.Views"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Key="ItemTemplate" x:DataType="model:SampleOrder">
<Grid Height="64" Padding="0,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<FontIcon Grid.Column="0" Tag="{x:Bind HashIdentIcon}" FontSize="40" Glyph="{x:Bind SymbolAsChar}" VerticalAlignment="Center" />
<StackPanel Grid.Column="1" Margin="12,0,0,0" VerticalAlignment="Center">
<TextBlock Text="{x:Bind Company}" Tag="{x:Bind HashIdentTitle}" Style="{ThemeResource ListTitleStyle}"/>
<TextBlock Text="{x:Bind Status}" Style="{StaticResource ListSubTitleStyle}"/>
</StackPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DetailsTemplate">
<views:MasterDetailDetailControl MasterMenuItem="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="NoSelectionContentTemplate">
<TextBlock x:Uid="MasterDetail_NoSelection" Style="{StaticResource ListNoSelectionTextStyle}" />
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition x:Name="TitleRow" Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock
x:Uid="MasterDetail_Title"
x:Name="TitlePage"
Margin="12,0,12,7"
Style="{StaticResource PageTitleStyle}" />
<controls:MasterDetailsView
Grid.Row="1"
x:Name="MasterDetailsViewControl"
ItemsSource="{x:Bind SampleItems}"
SelectedItem="{x:Bind Selected, Mode=OneWay}"
ItemTemplate="{StaticResource ItemTemplate}"
DetailsTemplate="{StaticResource DetailsTemplate}"
NoSelectionContentTemplate="{StaticResource NoSelectionContentTemplate}"
BorderBrush="Transparent">
<controls:MasterDetailsView.MasterCommandBar>
<CommandBar HorizontalAlignment="Left">
<AppBarButton Icon="Refresh" Label="Refresh"/>
<AppBarButton Icon="Add" Label="Add"/>
</CommandBar>
</controls:MasterDetailsView.MasterCommandBar>
<controls:MasterDetailsView.DetailsCommandBar>
<CommandBar HorizontalAlignment="Left">
<AppBarButton Icon="Edit" Label="Edit"/>
</CommandBar>
</controls:MasterDetailsView.DetailsCommandBar>
</controls:MasterDetailsView>
</Grid>
</Page>
命令栏位于底部(分别位于主面板和详细面板的底部)。我想让他们在顶部。我怎样才能做到这一点?
我尝试将主命令栏放在 MasterDetailsView 外面,将详细信息命令栏放在滚动查看器之前 MasterDetailDetailControl.xaml,但这看起来不太好,而且占用了不必要的空间 space。
您需要 re-template MasterDetailsView
.
- 您可以从 MasterDetailsView.xaml 获取默认模板
更改
中的行顺序<Grid.RowDefinitions>
<Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions>
将
MasterList
的行号更改为Grid.Row="2"
,将MasterCommandBarPanel
的行号更改为Grid.Row="1"
<Grid x:Name="MasterPanel" Width="{TemplateBinding MasterPaneWidth}" Background="{TemplateBinding MasterPaneBackground}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0,0,1,0"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ContentPresenter x:Name="HeaderContentPresenter" Margin="12,0" x:DeferLoadStrategy="Lazy" Content="{TemplateBinding MasterHeader}" ContentTemplate="{TemplateBinding MasterHeaderTemplate}" Visibility="Collapsed" /> <Grid x:Name="MasterCommandBarPanel" Grid.Row="1"></Grid> <ListView x:Name="MasterList" Grid.Row="2" IsTabStop="False" ItemContainerStyle="{TemplateBinding ItemContainerStyle}" ItemTemplate="{TemplateBinding ItemTemplate}" ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}" ItemsSource="{TemplateBinding ItemsSource}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" /> </Grid>
对DetailsPanel