在 phone 应用程序中选择数据模板控件时更改 GridView 的 SelectedItem

Change GridView's SelectedItem while selecting datatemplate control in phone application

MainPage.xaml 中的 GridView 绑定到 Employee 的 ObservableCollection。 Employee class 有 Amount(double) 属性 我想通过 TextBox 编辑。最后,当输入文本时,我想对剩余的 Employee 对象进行一些操作。我可以通过 INotifyPropetyChanged/PropertyChanged 获取编辑后的对象。但是我想我不能在这里执行操作,因为它会为我可能对其他对象执行的每个对象更新触发循环 PropertyChange?理想情况下,我应该依靠 TextBox 的 TextChanged 事件来执行此操作。

我面临的问题是我无法根据 GridView 的 SelectedItem(SelectedEmployee) 选择编辑的对象。只有当我 click/tap 它位于文本框之外和行内时,我才能设法选中它,但当我直接在文本框中单击时则不能。我想知道有没有办法在直接点击 TextBox 时 trigger/update GridView 的 SelectedItem?

低于我的MainPage.xaml

<storeApps:VisualStateAwarePage
x:Class="SimpleCurrency.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SimpleCurrency.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:storeApps="using:Microsoft.Practices.Prism.StoreApps"
xmlns:mvvm="using:Microsoft.Practices.Prism.Mvvm"    
mc:Ignorable="d"
mvvm:ViewModelLocator.AutoWireViewModel="True"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">


<GridView Margin="12,20,12,0" ItemsSource="{Binding Employees}" x:Name="grdEmployees"
          ItemTemplate="{StaticResource EmployeeGridTemplate}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}">        
</GridView>

和 DataTemplate EmployeeTemplate

    <DataTemplate x:Key="EmployeeGridTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ComboBox Width="120" ItemsSource="{Binding DataContext.Departments, ElementName=grdEmployees, Mode=TwoWay}" DisplayMemberPath="Code" SelectedValuePath="Code"/>

        <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=Amount, Mode=TwoWay}" InputScope="Number">
            <!--TODO: Need to get at text chagned event-->
            <interactivity:Interaction.Behaviors>
                <Core:EventTriggerBehavior EventName="TextChanged">
                    <Core:InvokeCommandAction Command="{Binding DataContext.ConvertCommand, ElementName=grdConversions}"
                                              CommandParameter="{Binding }"/>
                </Core:EventTriggerBehavior>
            </interactivity:Interaction.Behaviors>
        </TextBox>
    </Grid>
</DataTemplate>

向任何人开放suggestion/workaround

TextBox 的点击事件阻止 GridView 注意到点击事件,因为它是 "below" TextBox 的点击区域。 您应该能够确定哪个员工绑定到您的 TextBox(数据绑定上下文)并使用此信息以编程方式将 GridView 的选定项目设置为员工实例。