简体   繁体   English

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

[英]WP7 - How to get the list box items from ViewModel?

I am trying to get the selected name from list box when I click the list box button. 单击列表框按钮时,我试图从列表框中获取所选名称。

My XAML page:- 我的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>

Xaml.Cs:- Xaml.Cs: -

private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button myButton = sender as Button;
            ListBoxWithButtonModel dataObject = myButton.DataContext as ListBoxWithButtonModel;
            int index = MyListBox.Items.IndexOf(dataObject);
            MessageBox.Show("Button Clicked" + dataObject.FirstName);
        }

Here I can get the selected name when I click the button. 单击按钮后,可以在此处获得所选名称。 But this same thing I want to do from my View Model. 但是我想从我的视图模型中做同样的事情。 Please help me to get the selected name from ViewModel. 请帮助我从ViewModel获取选定的名称。

I have try like this. 我有这样的尝试。

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

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

            });

But here I am getting error DataContext does not contain ReactiveAsycCommand. 但是在这里我得到了错误DataContext不包含ReactiveAsycCommand。 Please help me to solve this problem. 请帮我解决这个问题。

My ViewModel:- 我的ViewModel:-

public ReactiveAsyncCommand buttonClick { get; set; }

public ListBoxWithButtonViewModel()
        {
            buttonClick = new ReactiveAsyncCommand();
            buttonClick.Subscribe(x =>
            {
                MessageBox.Show("TEst");
            });
        }

Herer I can show the message box. 在这里,我可以显示消息框。 But how to get the selected item here?? 但是如何在这里获取所选的物品呢?

My Another try. 我的另一个尝试。

 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);
        }

Here also I can not get the selected item. 我在这里也无法获得所选项目。 Please give me any idea to resolve this problem. 请给我任何解决此问题的想法。

It isn't stated clearly but I suspect the problem is myItem parameter always null because you never pass the parameter in XAML. 没有明确说明,但我怀疑问题是myItem参数始终为null因为您从未在XAML中传递该参数。

Use CommandParameter property to pass parameter for your Command , for example : 使用CommandParameter属性为Command传递参数,例如:

<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>

Set the DataContext to the button. 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>

Take a look here Windows Phone 7 - Function for ListBox button 在这里看看Windows Phone 7-ListBox的功能按钮

Hope this helps :) 希望这可以帮助 :)

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM