【问题标题】:Windows Phone 7 - Function for ListBox buttonWindows Phone 7 - ListBox 按钮的功能
【发布时间】:2014-04-25 09:00:55
【问题描述】:

我在 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”的编码。

我的视图模型:-

 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());
    });
}

但在这里我无法显示所选项目。
请告诉我解决此问题的任何想法。

【问题讨论】:

  • 请贴出addPerson的声明是字段还是属性?
  • 嗨@Verdsrobert。我在原始问题中添加了addPerson 声明。
  • 我知道我要说的不是太多 MVVM,只是为了测试您的命令是否已绑定,您可以订阅 Button 上的 Loaded 事件并检查它的命令属性是否已设置,并且它的值等于你的 addPerson ReactiveAsyncCommand。
  • 嗨@verdesrobert..你能给我举个例子吗?

标签: c# windows-phone-7 listbox


【解决方案1】:

确保您的代码正常运行 做一些这样的测试:

<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");
        }
    }
}

【讨论】:

  • 嗨@verdesrobert。谢谢你的答复。此代码不起作用。我还想在ViewModel 中编写这个函数。还有其他想法吗?
  • 请详细说明“此代码无效”
  • 嗨@verdesrobert .. 它显示了我的班级名称。我也尝试过使用 dii 属性。它没有显示列表项名称。
  • 尝试在您的 Button XAML 定义中将 DataContext 移动到 Command 之前
  • 好的.. 完成..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
相关资源
最近更新 更多