【发布时间】:2020-05-18 16:47:07
【问题描述】:
我正在尝试处理我的 UWP 应用的一个小问题。在接下来的页面中,当使用 AppBarButton Edit 时,我想将 DataGrid 的选定项绑定为 CommandParameter 但它不起作用(使用空参数调用该命令)。出于故障排除的目的,我创建了 TextBlock MyTextBlock,其 Text 属性绑定到 DataGrid 的 SelectedItem。但即使选择了一个项目,TextBlok 也不会更新。您是否发现以下 XAML 页面有问题? 多年来,我一直在使用 WPF 进行此操作,但无法使其与 UWP 一起使用。
提前感谢您的建议。
<Page x:Name="MyPage"
x:Class="MyApp.PageSites"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WindowsTechnicianClient"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uwp="using:Microsoft.Toolkit.Uwp.UI.Controls"
xmlns:base="using:Base"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<local:StaticMapper x:Key="Static"/>
</Page.Resources>
<Page.TopAppBar>
<CommandBar ClosedDisplayMode="Compact" IsOpen="True">
<CommandBar.PrimaryCommands>
<AppBarElementContainer>
<TextBlock x:Name="MyTextBox" Text="{Binding ElementName=MyDataGridSites, Path=SelectedItem}"/>
</AppBarElementContainer>
<AppBarButton Command="{x:Bind CommandEditItem}" CommandParameter="{Binding ElementName=MyDataGridSites, Path=SelectedItem}"
Icon="Edit" IsCompact="False" Label="Edit"/>
</CommandBar.PrimaryCommands>
</CommandBar>
</Page.TopAppBar>
<uwp:DataGrid x:Name="MyDataGridSites" AlternatingRowBackground="WhiteSmoke" AutoGenerateColumns="False"
IsReadOnly="True" ItemsSource="{Binding Source={StaticResource Static}, Path=Sites}" SelectionMode="Single">
<uwp:DataGrid.Columns>
<uwp:DataGridTextColumn Binding="{Binding}" Header="Name"/>
<uwp:DataGridTextColumn Binding="{Binding Path=Type}" Header="Type"/>
<uwp:DataGridTextColumn Binding="{Binding Path=Address}" Header="Address"/>
<uwp:DataGridTextColumn Binding="{Binding Path=Latitude}" Header="Latitude"/>
<uwp:DataGridTextColumn Binding="{Binding Path=Longitude}" Header="Longitude"/>
</uwp:DataGrid.Columns>
</uwp:DataGrid>
【问题讨论】: