WP7 - 单击列表框项目时调用绑定编号

时间:2010-11-19 22:18:50

标签: listbox windows-phone-7

所以我有一个对象列表,其中包含绑定到XAML中列表框的名称和编号。列表框显示数字正常,但我希望它在点击时使用电话呼叫拨打电话号码。这是onlick代码:

    private void taxiListItem_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Phone.Tasks.PhoneCallTask phonecall = new Microsoft.Phone.Tasks.PhoneCallTask();
        phonecall.PhoneNumber = "213";
        phonecall.Show();
    }

这里是我定义填充列表的TaxiCompany对象的地方。

    public class TaxiCompany {
        public String CoName { get; set; }
        public String Phone { get; set; }
        public TaxiCompany(String coname, String phone) {
            this.CoName = coname;
            this.Phone = phone;

        }
    }

当我对数字进行硬编码时,电话呼叫正常。现在,当我设置phonecall.Phonenumber = sender.getPhone()或e.Phone()或两者的任何变体时,它被标记为未定义的方法。我在这里做了一些根本错误的事吗?我假设对象发送者或e是被点击的列表项。

注意:XAML中的列表框显示电话号码和地址

                   <Button Click="taxiListItem_Click" Width ="436" Height="120">
                        <Button.Content>
                            <StackPanel Orientation="Vertical" Height=" 80">
                            <StackPanel Orientation="Horizontal" Height="40" Width="436">
                                <TextBlock Width="436" FontSize="30" Text= "{Binding CoName}" Height="40"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Height="40" Width="436">
                                <TextBlock Name ="PhoneNo"  Width="300" FontSize="22" Text= "{Binding Phone}" Height="40"/>
                            </StackPanel>

                            </StackPanel>    
                        </Button.Content>
                    </Button>

首次使用C#/ Silverlight,以便提供任何帮助。

1 个答案:

答案 0 :(得分:4)

试试这个。列表框显示出租车公司,并在列表中选择一个项目导致手机号码腐烂尝试并拨打。

请注意,没有必要向列表添加按钮,并且必须转换传递给选择事件的变量。 (也可以将发件人转换为列表框,然后投射所选项目。)

XAML:

<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding TaxiCompanies}" SelectionChanged="MainListBox_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Height=" 80">
                <TextBlock Width="436" FontSize="30" Text= "{Binding CoName}" Height="40"/>
                <TextBlock Name ="PhoneNo"  Width="300" FontSize="22" Text= "{Binding Phone}" Height="40"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

CS:

// class used for example. Another name would be more appropriate
public class ViewModel
{
    public ObservableCollection<TaxiCompany> TaxiCompanies { get; private set; }

    public ViewModel()
    {
        TaxiCompanies = new ObservableCollection<TaxiCompany>();
        TaxiCompanies.Add(new TaxiCompany("AAA Cabs", "123-456-789"));
        TaxiCompanies.Add(new TaxiCompany("BBB Taxis", "111234329"));
        TaxiCompanies.Add(new TaxiCompany("CCC Cars", "98765432"));
    }
}

    public MainPage()
    {
        InitializeComponent();

        // Set the data context of the listbox control to the sample data
        DataContext = new ViewModel();

        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var phonecall = new PhoneCallTask();
        phonecall.PhoneNumber = ((TaxiCompany)(((object[])(e.AddedItems))[0])).Phone;
        phonecall.Show();

        // Reset selected index to -1 (no selection)
        MainListBox.SelectedIndex = -1;
    }