【问题标题】:UWP DataGrid SelectedItem binding not workingUWP DataGrid SelectedItem 绑定不起作用
【发布时间】: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>

【问题讨论】:

    标签: uwp datagrid uwp-xaml


    【解决方案1】:

    我想通了: 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>
    

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 2012-10-12
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-07
      • 2017-04-30
      • 2018-03-15
      • 1970-01-01
      相关资源
      最近更新 更多