当我单击列表框按钮时,我试图从列表框中获取所选名称。
我的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);
}
这里我可以在单击按钮时获取所选名称。但是我想从View Model中做同样的事情。请帮我从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);
});
但是我在这里收到错误DataContext不包含ReactiveAsycCommand。 请帮我解决这个问题。
我的ViewModel: -
public ReactiveAsyncCommand buttonClick { get; set; }
public ListBoxWithButtonViewModel()
{
buttonClick = new ReactiveAsyncCommand();
buttonClick.Subscribe(x =>
{
MessageBox.Show("TEst");
});
}
Herer我可以显示消息框。但是如何在这里获取所选项目?
我的另一次尝试。
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);
}
这里我也无法获得所选项目。请给我任何解决此问题的建议。
答案 0 :(得分: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>
答案 1 :(得分:0)
将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
希望这有帮助:)
答案 2 :(得分:0)
试试这个:
private void Button_Click(object sender, RoutedEventArgs e)
{
StudentDetails obj = MyListBox.SelectedItem;
MessageBox.Show(obj.FirstName);
}