我在 ListBox 中有项目列表。
所有项目都有按钮。
我想在单击列表框内的按钮时显示所选项目的名称(不是列表框项目)。
我的 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”编写了代码。
我的 View 模型:-
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));
}
}
}
这里我可以在单击列表框项目时显示选定的名称。
但是现在我想在单击按钮时显示。
单击按钮时是否可以显示项目?
现在我试过这样:-
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());
});
}
但是这里我不能显示选中的项目。
请告诉我解决此问题的任何想法。
最佳答案
确保您的代码正常工作 做一些这样的测试:
<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");
}
}
}
关于c# - Windows Phone 7 - 列表框按钮的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23288635/