[英]How to select the listbox items from View Model
I want to select the list box items from View Model? 我想从视图模型中选择列表框项吗?
My xaml is:- 我的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>
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. 但是我可以从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 但我收到此错误:- 在当前上下文中不存在名称“发送方”
In xaml in ListBox: 在ListBox的xaml中:
SelectedItem={Binding SelectedStudent, Mode=TwoWay}
Then in ViewModel: 然后在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"); }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.