【问题标题】:WP7 - How to get the list box items from ViewModel?WP7 - 如何从 ViewModel 获取列表框项目?
【发布时间】:2014-08-09 14:49:24
【问题描述】:

当我单击列表框按钮时,我试图从列表框中获取选定的名称。

我的 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:-

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

在这里我可以在单击按钮时获取所选名称。但是我想从我的视图模型中做同样的事情。请帮我从 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);

            });

但是在这里我收到错误数据上下文不包含 ReactiveAsycCommand。 请帮我解决这个问题。

我的视图模型:-

public ReactiveAsyncCommand buttonClick { get; set; }

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

在这里,我可以显示消息框。但是这里怎么获取选中的item呢??

我的另一个尝试。

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

在这里我也无法获取所选项目。请给我任何想法来解决这个问题。

【问题讨论】:

  • 您想在视图模型的哪个位置访问选定的名称?
  • @har07.. 我已经更新了我的问题。请查看并给我解决方案。
  • 你更新的代码有什么问题,是myItem总是null吗?
  • 是的..它总是空的..

标签: c# xaml windows-phone-7 mvvm listbox


【解决方案1】:

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

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

【讨论】:

  • 看不出我的代码有什么问题,它对我来说很好用。确保在此答案之后更新 Button 中的所有绑定。
  • 嗨@har07..我无法得到答案。根据您的指示,我更改了我的 XAML。 '
  • 嗨@har07.. 我已经上传了一个示例项目。如果可能,请修改它?对不起,如果我问错了什么.. [onedrive.live.com/redir?resid=9CBE7976CDD57B70%21191]
  • 嗨@har07.. 你看到我的例子了吗??
  • 刚试了下项目,链接好像已经过期了
【解决方案2】:

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>

看这里Windows Phone 7 - Function for ListBox button

希望这会有所帮助:)

【讨论】:

  • @Vijay 你如何在这里绑定你的视图模型?
【解决方案3】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多