C# WP7-如何从ViewModel获取列表框项目?

C# WP7-如何从ViewModel获取列表框项目?,c#,xaml,windows-phone-7,mvvm,listbox,C#,Xaml,Windows Phone 7,Mvvm,Listbox,单击列表框按钮时,我正在尝试从列表框中获取所选名称 我的XAML页面:- <ListBox Name="MyListBox" Height="733" ItemsSource="{Binding StudentDetails,Mode=TwoWay}" HorizontalAlignment="Left" Margin="0,35,0,0" VerticalAlignment="Top" Wi

单击列表框按钮时,我正在尝试从列表框中获取所选名称

我的XAML页面:-

    <ListBox Name="MyListBox"  Height="733" ItemsSource="{Binding StudentDetails,Mode=TwoWay}" 
                     HorizontalAlignment="Left" Margin="0,35,0,0" 
                     VerticalAlignment="Top" Width="476">
                <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"  />
                                <TextBlock Text="{Binding LastName}" Name="lastName" Width="200" Foreground="White" Margin="-200,50,0,0" FontWeight="SemiBold" FontSize="22"  />
                                <TextBlock Text="{Binding Age}" Name="age" Width="200" Foreground="White" Margin="10,10,0,0" FontWeight="SemiBold" FontSize="22"  />


   <Button Command="{Binding buttonClick}" DataContext="{Binding DataContext, ElementName=MyListBox}" Margin="-200,0,0,0" Height="80" Width="80">         
                                    <Button.Background>
                                        <ImageBrush Stretch="Fill" >
                                            <ImageBrush.ImageSource>
                                                <BitmapImage UriSource="/NewExample;component/Images/icon_increase.png" />
                                            </ImageBrush.ImageSource>
                                        </ImageBrush>
                                    </Button.Background>                                   
                                </Button>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
在这里,我可以在单击按钮时获得所选名称。但我想从我的视图模型中做同样的事情。请帮助我从ViewModel获取所选名称

我试过这样做

 listButton = new ReactiveAsyncCommand();
            listButton.Subscribe(x =>
            {

                ListBoxWithButtonModel dataObject = listButton.DataContext as ListBoxWithButtonModel;
                //int index = MyListBox.Items.IndexOf(dataObject);
                MessageBox.Show("Button Clicked" + dataObject.FirstName);

            });
但这里我得到的错误是DataContext不包含ReactiveAsycCommand。 请帮我解决这个问题

我的ViewModel:-

public ReactiveAsyncCommand buttonClick { get; set; }

public ListBoxWithButtonViewModel()
        {
            buttonClick = new ReactiveAsyncCommand();
            buttonClick.Subscribe(x =>
            {
                MessageBox.Show("TEst");
            });
        }
在这里我可以显示消息框。但是如何在这里获取所选项目

我的另一次尝试

 public RelayCommand<ListBoxWithButtonModel> ItemSelectedCommand { get; private set; }

 public ListBoxWithButtonViewModel()
        {
            ItemSelectedCommand = new RelayCommand<ListBoxWithButtonModel>(ItemSelected);
        }

        private void ItemSelected(ListBoxWithButtonModel myItem)
        {
            MessageBox.Show("Testing");
            if (null != myItem)
                MessageBox.Show("Name==>" + myItem.FirstName);
        }
public RelayCommand ItemSelectedCommand{get;private set;}
public ListBoxWithButtonViewModel()
{
ItemSelectedCommand=新建中继命令(ItemSelected);
}
已选择私有无效项(ListBoxWithButtonModelMyItem)
{
MessageBox.Show(“测试”);
如果(null!=myItem)
MessageBox.Show(“Name==>”+myItem.FirstName);
}

在这里,我也无法获取所选项目。请告诉我解决此问题的方法。

DataContext
设置为该按钮

<Button DataContext="{Binding DataContext, ElementName=MyListBox}"
Height="80"
Width="80"
Command="{Binding listButton}">
<Button.Background>
<ImageBrush ImageSource="/NewExample;component/Images/icon_increase.png"
            Stretch="Fill" />
</Button.Background>
</Button>

看看这里


希望这有帮助:)

它没有明确说明,但我怀疑问题在于
myItem
参数总是
null
,因为您从未在XAML中传递参数

使用
CommandParameter
属性传递
命令的参数,例如:

<Button Command="{Binding DataContext.ItemSelectedCommand, ElementName=MyListBox}" 
        CommandParameter="{Binding}"
        Margin="-200,0,0,0" Height="80" Width="80">         
    <Button.Background>
        <ImageBrush Stretch="Fill" >
            <ImageBrush.ImageSource>
                <BitmapImage UriSource="/NewExample;component/Images/icon_increase.png" />
            </ImageBrush.ImageSource>
        </ImageBrush>
    </Button.Background>                                   
</Button>

试试这个:

private void Button_Click(object sender, RoutedEventArgs e)
{
    StudentDetails obj = MyListBox.SelectedItem;
    MessageBox.Show(obj.FirstName);
}

您想在viewmodel中的哪个位置访问选定的名称?@har07。。我已经更新了我的问题。请检查并给我解决方案。问题是什么?您更新的代码是什么,是
myItem
始终
null
?是的。。它总是空的。@Vijay您如何在这里绑定您的viewmodel?看不出我的代码有什么问题,它对我很好。确保在回答此问题后更新
按钮中的所有绑定。Hi@har07。。我得不到答案。按照你的指示,我已经更改了我的XAML。但没有改善。请让我知道我哪里做错了。嗨@har07。。我上传了一个示例项目。如果可能的话,请修改一下?。对不起,如果我问错了什么。。[Hi@har07..你看到我的例子了吗?刚刚试过,但我无法获得项目,链接似乎已经过期了
private void Button_Click(object sender, RoutedEventArgs e)
{
    StudentDetails obj = MyListBox.SelectedItem;
    MessageBox.Show(obj.FirstName);
}