在手机应用程序中选择datatemplate控件时,更改GridView的SelectedItem

时间:2015-07-28 02:56:07

标签: xaml windows-phone-8.1 windows-store-apps

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

我面临的问题是我无法根据GridView的SelectedItem(SelectedEmployee)选择已编辑的对象。只有当我在TextBox外部和行内单击/点击它而不是直接在TextBox中单击时,我才能设法选择它。我想知道有一种方法可以在直接点击TextBox时触发/更新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>

对任何建议/解决方法开放

1 个答案:

答案 0 :(得分:0)

TextBox的tap事件阻止了GridView注意到tap事件,因为它在&#34;下面&#34; TextBox点击区域。 您应该能够确定哪个员工绑定到TextBox(数据绑定上下文),并使用此信息以编程方式将GridView的选定项目设置为Employee Instance。