【问题标题】:How to select the listbox items from View Model如何从视图模型中选择列表框项目
【发布时间】:2014-04-23 11:50:27
【问题描述】:

我想从视图模型中选择列表框项?

我的 xaml 是:-

<Grid x:Name="MVVMListBoxUIContainer" Grid.Row="1" Margin="2,0,2,0">
<ListBox Height="374" ItemsSource="{Binding StudentDetails,Mode=TwoWay}" HorizontalAlignment="Left" Margin="2,6,0,0" Name="listBox1" VerticalAlignment="Top" Width="476" BorderBrush="#00410D0D">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Tap">
            <i:InvokeCommandAction Command="{Binding EventPageCommand, Mode=OneWay}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <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"  />

                     <Button Margin="-100,0,0,0" Height="80" Width="80" DataContext="{Binding DataContext, ElementName=listBox1}" Command="{Binding addPerson}">
                        <Button.Background>
                            <ImageBrush ImageSource="{Binding addImage}" Stretch="Fill" />
                        </Button.Background>
                    </Button>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的视图模型:-

EventPageCommand = new ReactiveAsyncCommand();
EventPageCommand.Subscribe(x =>
{
    MessageBox.Show("List box item is clicked.");
});

在这里,当我单击列表框项目时,消息框正在显示。但我无法显示所选项目。 但我可以从 Xaml.CS 显示。

private void listBox1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    ListBox listbox = (ListBox)sender;
    ListBoxEventsModel items = (ListBoxEventsModel)listbox.SelectedItem;
    MessageBox.Show("Selected Name"+items.FirstName);
    //if(!Constants.buttonSelected)
    //MessageBox.Show("List item is selected..!!");
}

但是当我从视图模型中尝试时,我无法显示所选项目。请让我知道如何显示所选项目。 我试过这样:-

EventPageCommand = new ReactiveAsyncCommand();
EventPageCommand.Subscribe(x =>
{
    MessageBox.Show("List box item is clicked.");

    ListBox listbox = (ListBox)sender;
    ListBoxEventsModel items = (ListBoxEventsModel)listbox.SelectedItem;
    MessageBox.Show("Selected Name" + items.FirstName);
});

但我收到此错误:- 名称“发件人”在当前上下文中不存在

【问题讨论】:

  • 您必须在 ListBox 中使用参数 Mode=TwoWay 绑定 SelectedItems
  • 嗨@Musketyr。谢谢您的回复。我是 Windows 手机的新手。你能给我举个例子吗(对不起)?

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


【解决方案1】:

在 ListBox 中的 xaml 中:

SelectedItem={Binding SelectedStudent, Mode=TwoWay}

然后在 ViewModel 中:

public ObservableCollection&lt;YOUR_OBJECT_HERE&gt; SelectedItems { get; set;}

编辑完整示例:

 <ListBox Margin="5,0" Grid.Row="1" Grid.RowSpan="2" ItemsSource="{Binding Roles}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Title}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

视图模型

public class RolesViewModel : BaseViewModel
{
    private Collection<BO.RoleBO> roles;
    private BO.RoleBO selectedItem;

    public BO.RoleBO SelectedItem
    {
        get { return selectedItem; }
        set 
        { 
            selectedItem = value;
            NotifyOnPropertyChanged("SelectedItem"); 
        }
    }

    public Collection<BO.RoleBO> Roles
    {
        get { return roles; }
        set { roles = value; NotifyOnPropertyChanged("Roles"); }
    }

}

【讨论】:

  • 嗨..我已经尝试过你的想法。但不工作.. public static ObservableCollection _SelectedStudent;公共 ObservableCollection SelectedStudent { get { return _SelectedStudent; } 设置 { this.RaiseAndSetIfChanged(x => x.SelectedStudent, value); MessageBox.Show("选中"); } }
  • 你有没有调用构造函数:new ObservableCollection...?
  • 嗨@Musketyr。不,对不起。请给我举个例子?
  • 嗨@Dhaval..这是我想要的..谢谢..!!
猜你喜欢
  • 2012-03-25
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 2013-08-03
  • 2016-10-26
  • 1970-01-01
相关资源
最近更新 更多