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

时间:2014-05-10 09:26:19

标签: xaml windows-phone-7 mvvm listbox reactiveui

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

现在我尝试这样: -

   <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 ListBoxEventsViewModel()
        {            
            addPerson = new ReactiveAsyncCommand();
            addPerson.Subscribe(x =>
            {
                MessageBox.Show("Test Button Selected..");
            });

        }

点击此按钮,我可以在此处显示消息框。但我无法选择列表项。请让我知道解决这个问题。

提前致谢。

查看型号: - 选择列表框项目: -

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

此处显示单击列表项时所选的名称。但同样的事情我想写它为按钮(addImage)

1 个答案:

答案 0 :(得分:0)

将ListBox上的SelectedItem绑定到&#34; SelectedItem&#34; ViewModel上的属性。

例如SelectedItem =&#34; {Binding SelectedItem,Mode = TwoWay}&#34;

然后在视图模型上添加一个SelectedItem的属性,然后你可以在你的订阅中设置它。