Question

I want to select the list box items from View Model?

My xaml is:-

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

My View Model:-

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

Here when I click on the list box items the message box is showing. But I can not show the selected items. But I can show from 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..!!");
}

But when I try it from View Model I can not show the selected Items. Please let me know how to show the selected items. I have try like this:-

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

But I am receiving this error:- The name 'sender' does not exist in the current context

Était-ce utile?

La solution

In xaml in ListBox:

SelectedItem={Binding SelectedStudent, Mode=TwoWay}

Then in ViewModel:

public ObservableCollection<YOUR_OBJECT_HERE> SelectedItems { get; set;}

EDIT Full example:

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

ViewModel

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

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top