c# - Windows Phone 7 - 列表框按钮的功能

标签 c# windows-phone-7 listbox

我在 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/

相关文章:

c# - 如何使用 LINQ 在 db 上下文中为 3 个不同的表在 asp .net mvc 中编写通用查询?

c# - 应用程序域级模拟

WPF:删除样式列表框中焦点项目周围的虚线边框

c# - 如何返回一个带有我事先不知道的通用参数的函数?

windows-phone-7 - 全景 wp7 mvvm 中的静态和动态全景项目

c# - 在后台线程 WP7 上创建 BitmapImage

linq-to-sql - 为什么 LINQ to SQL 数据库没有保留在 WP7 模拟器中?

vba - 设置 Listindex 后 Excel 列表框不一致地没有值

c# - 使用自定义 ItemsPanel 模板的 WPF ListBox 如何根据其项目的大小调整自身大小

C#如何将IEnumerable匿名列表转换成数据表