Reputation: 137
I'm trying to deal with a small issue with my UWP app. In the following page, when using the AppBarButton Edit, I'd like to bind the selected item of the DataGrid as CommandParameter but it's not working (the command is called with a null parameter). For a troubleshooting purpose, I've created the TextBlock MyTextBlock whose Text property is bound to the DataGrid's SelectedItem. But even after selecting an item, the TextBlok is not updated. Do you see something wrong with the following XAML page? I has been doing that for years with WPF but cannot get it work with UWP.
Thank you in advance for your suggestions.
<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>
Upvotes: 0
Views: 369
Reputation: 137
I figured this out: CommandParameter="{x:Bind MyDataGridSites.SelectedItem, Mode=OneWay}"
<Page.Resources>
<local:StaticMapper x:Key="Static"/>
</Page.Resources>
<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>
<Page.TopAppBar>
<CommandBar ClosedDisplayMode="Compact" IsOpen="True">
<CommandBar.PrimaryCommands>
<AppBarButton Command="{x:Bind CommandEditItem}" CommandParameter="{x:Bind MyDataGridSites.SelectedItem, Mode=OneWay}"
Icon="Edit" IsCompact="False" Label="Edit"/>
</CommandBar.PrimaryCommands>
</CommandBar>
</Page.TopAppBar>
Upvotes: 3