c# - Button Click to get an Object on SelectionChanged 事件

标签 c# windows-phone-7 uri parameter-passing selectionchanged

我有一个 SelectionChanged 事件并且工作得很好,但我想弄清楚如何在单击按钮时“捕获”这个选定的项目,他们需要将它作为参数传递给另一个页面,并且编辑此项目。这是我仍然实现的当前代码和按钮 SelectionChanged,因为这是我需要的。

private void listCarros_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        ListBox listBox = sender as ListBox;

        if (listBox != null && listBox.SelectedItem != null)
        {
            //pega o Carro que foi selecionado
            Carro sCar = (Carro)listBox.SelectedItem;

            btnEditCar.IsEnabled = true;
            btnDeleteCar.IsEnabled = true;
        }
        else
        {
            btnEditCar.IsEnabled = false;
            btnDeleteCar.IsEnabled = false;
        }
    }

我需要编辑这个按钮上的 selectedItem:

private void btnEditCar_Click(object sender, EventArgs e)
{
    //Here I need access to the selectedItem on SelectionChanged event.  
}

如果你也能告诉我如何将对象作为参数传递就完美了。

最佳答案

You can do this with binding also

1.Bind ListBoxItem(Carro Object) to the tag of "btnEditCar" in xaml.

Xaml should be like this

<Button Name="btnEditCar" OnClick="btnEditCar_Click" Tag="{Binding}"/>

现在在

private void btnEditCar_Click(object sender, EventArgs e)
{
    Carro sCar=(Carro)((sender as FrameworkElement).Tag)
}

这是很好的做法,只为临时目的创建一个类变量是 hack

关于c# - Button Click to get an Object on SelectionChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360414/

相关文章:

c# - 在字符串中替换“的问题

c# - 在 WINRT 应用程序中使用转换器

c# - 在线程池线程的 Windows Phone 7 上调整图像大小?

windows - 运行标签不支持 WP7 中的数据绑定(bind)?

java - 将 SoundPool 与 URI 一起使用?

c# - 如何快速搜索单词或数字 c#

c# - 在 Devart Connectory Mysql 中使用事务作用域的正确方法

android - Android 中的 Uri 是什么?

uri - 如果有的话,什么时候应该在 URL 中对像 { 和 } (大括号)这样的字符进行百分比编码?

c# - WebBrowser 控件上的选定文本颜色和背景颜色