Windows Phone 7-“列表框"按钮的功能 [英] Windows Phone 7 - Function for ListBox button
问题描述
我在ListBox中有项目列表.
所有项目都有按钮.
当我单击列表框内的按钮(而不是列表框项)时,我想显示所选项目的名称.
I have list of items in ListBox.
All the items have buttons.
I want to show the selected items name when I click the button inside the ListBox (Not the list box items).
我的Xaml:-
<ListBox SelectedIndex="{Binding SelectedIndex,Mode=TwoWay}"
SelectedItem="{Binding SelectedStudent, Mode=TwoWay}" 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 Height="80" Width="80" Command="{Binding addPerson}"
DataContext="{Binding DataContext, ElementName=listBox1}">
<Button.Background>
<ImageBrush
ImageSource="/NewExample;component/Images/icon_increase.png" />
</Button.Background>
</Button>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在这里,当我单击"addPerson"按钮时,它应显示所选项目的名称.
现在,我为列表框"SelectedItmes"编写了代码.
Here when I click the 'addPerson' Button it should show the selected items name.
Now I have write the coding for List box 'SelectedItmes'.
我的ViewModel:-
My ViewModel:-
private MVVMListBoxModel selectedStudent;
public MVVMListBoxModel SelectedStudent
{
get { return selectedStudent; }
set
{
if (selectedStudent == value)
return;
selectedStudent = value;
MessageBox.Show("Selected Item==>" + selectedStudent.FirstName + " Selected Index==>" + SelectedIndex);
if (SelectedIndex == 0)
{
var rootFrame = (App.Current as App).RootFrame;
rootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
}
}
在这里,当我单击列表框项目时,可以显示所选的名称.
但是现在我想在单击按钮时显示.
单击按钮时是否可以显示项目?
Here I can show the selected Name when I click the list box items.
But now I want to show when I click the button.
Is it possible to show the items when I click the button?
现在我已经尝试过这样:-
Now I have tried like this:-
public ReactiveAsyncCommand addPerson { get; set; }
public MVVMListBoxViewModel()
{
addPerson = new ReactiveAsyncCommand();
addPerson.Subscribe(x =>
{
MessageBox.Show("Button Clicked..!!");
ListBox listbox = (ListBox)sender;
ListBoxEventsModel items = (ListBoxEventsModel)listbox.SelectedItem;
MessageBox.Show("Selected Name" + items.ToString());
});
}
但是在这里我无法显示所选的项目.
请让我有任何办法解决此问题.
But here I can not show the selected items.
Please let me any idea to solve this issue.
推荐答案
确保您的代码正常运行 进行如下测试:
To make sure that your code is working Make some tests like this:
<Button Tap="btnTestCommand_Tap" Height="80" Width="80" Command="{Binding addPerson}"
DataContext="{Binding DataContext, ElementName=listBox1}">
<Button.Background>
<ImageBrush ImageSource="/NewExample;component/Images/icon_increase.png" />
</Button.Background>
</Button>
后面的代码:
private void btnTestCommand_Tap(object sender, GestureEventArgs e)
{
Button btn=sender as Button;
var command = btn.Command;
MessageBox.Show("Entering Command Check\n DataContext is of type: "+btn.DataContext.GetType().Name);
if(command !=null)
{
MessageBox.Show("Command is not null");
if(command is ReactiveAsyncCommand)
{
MessageBox.Show("Command is of ReactiveAsyncCommand type");
}
}
}
这篇关于Windows Phone 7-“列表框"按钮的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!