Xaml WP7-是否可以在单击按钮时从列表中选择项目

Xaml WP7-是否可以在单击按钮时从列表中选择项目,xaml,windows-phone-7,mvvm,listbox,reactiveui,Xaml,Windows Phone 7,Mvvm,Listbox,Reactiveui,我想在单击按钮时选择列表项(位于每行的列表框内) 现在我试着这样做:- <ListBox Height="444" ItemsSource="{Binding StudentDetails,Mode=TwoWay}" HorizontalAlignment="Left" Margin="2,34,0,0" Name="listBox1" VerticalAl

我想在单击按钮时选择列表项(位于每行的列表框内)

现在我试着这样做:-

   <ListBox Height="444"
                     ItemsSource="{Binding StudentDetails,Mode=TwoWay}" 
                     HorizontalAlignment="Left" Margin="2,34,0,0"
                     Name="listBox1" VerticalAlignment="Top" 
                     Width="476" BorderBrush="#00410D0D" 
                     SelectedIndex="{Binding MemberPrivacy,Mode=TwoWay}"
                     SelectedItem="{Binding SelectedStudent, Mode=TwoWay}">               
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Gray" Padding="5" BorderThickness="1">
                            <StackPanel Orientation="Horizontal">
                                <Border BorderBrush="Wheat" BorderThickness="1">
                                    <Image  Name="ListPersonImage" Source="{Binding PersonImage}" Height="100" Width="100" Stretch="Uniform" Margin="10,0,0,0"/>
                                </Border>
                                <TextBlock Text="{Binding FirstName}" Name="firstName" Width="200" Foreground="White" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22"  />
                                <Image Height="50" Source="{Binding addImage}" HorizontalAlignment="Left"  Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="50" />

                                <Button Margin="-100,0,0,0" Height="80" Width="80" DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}" >
                                    <Button.Background>
                                        <ImageBrush ImageSource="{Binding addImage,  Converter={StaticResource pathToImageConverter}}" Stretch="Fill" />
                                    </Button.Background>
                                </Button>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox> 
在这里,我可以在单击按钮时显示消息框。但我无法选择列表项。请让我知道如何解决这个问题

提前谢谢

视图模型:- 列表框所选项目:-

public ListBoxEventsModel _SelectedStudent;
        public ListBoxEventsModel SelectedStudent
        {
            get { return _SelectedStudent; }
            set
            {
                this.RaiseAndSetIfChanged(x => x.SelectedStudent, value);
                MessageBox.Show("Selected index==>" + SelectedStudent.FirstName);
            }
        }

在这里,当我单击列表项时,它将显示所选名称。但是,我想为按钮(addImage)编写同样的代码。

将列表框上的SelectedItem绑定到ViewModel上的“SelectedItem”属性

例如SelectedItem=“{Binding SelectedItem,Mode=TwoWay}”


然后在视图模型上添加SelectedItem属性,然后您可以在subscribe中设置该属性

另外需要注意的是,在更高版本的ReactiveUI中,ReactiveAsyncCommand被降级并与ReactiveCommand.Hi@Glenn Watson结合使用。我听从你的指示。我可以在单击列表项目时选择该项目。但我只想用这个按钮。我想为列表框编写两个函数。一个用于列表项。另一个按钮。两者都是不同的功能。请帮我解决这个问题。你好,格伦。。我已经按照您的指示添加了编码。。请参考,并给我任何解决我的问题的想法。在这里,我面临着另一个问题。列表框项在第二次单击时没有响应。只要让绑定引用SelectedItem绑定,在按下按钮之前,您不需要任何代码执行任何操作。
public ListBoxEventsModel _SelectedStudent;
        public ListBoxEventsModel SelectedStudent
        {
            get { return _SelectedStudent; }
            set
            {
                this.RaiseAndSetIfChanged(x => x.SelectedStudent, value);
                MessageBox.Show("Selected index==>" + SelectedStudent.FirstName);
            }
        }