Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wrappanel列表框项选择事件_C#_Silverlight_Windows Phone 7_Listbox_Wrappanel - Fatal编程技术网

C# wrappanel列表框项选择事件

C# wrappanel列表框项选择事件,c#,silverlight,windows-phone-7,listbox,wrappanel,C#,Silverlight,Windows Phone 7,Listbox,Wrappanel,我使用wrappanel和listbox在wp7上显示我的项目。但项目单击事件不起作用。我的代码如下 <Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto"> <ListBox x:Name="lstDevice"> <ListBox.ItemsPanel> <ItemsPanelTemplate>

我使用wrappanel和listbox在wp7上显示我的项目。但项目单击事件不起作用。我的代码如下

<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto">
            <ListBox x:Name="lstDevice">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel/>                        
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate >
                        <StackPanel>
                            <Button x:Name="btnData" >
                                <StackPanel Orientation="Vertical">
                                    <Canvas 
                                            Width="175" 
                                            Height="175"/>                                            
                                    <TextBlock Text="{Binding Name}" Width="175" />
                                </StackPanel>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

如何生成listbox项选择事件并获取数字或某些属性以识别所选的项?提前谢谢

我认为这可能更适合你:

<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto">
            <ListBox x:Name="lstDevice">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel/>                        
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate >
                            <Button x:Name="btnData" Click="OnButtonClick" Tag="{Binding Name}" >
                                <StackPanel Orientation="Vertical">
                                    <Canvas 
                                            Width="175" 
                                            Height="175"/>                                            
                                    <TextBlock Text="{Binding Name}" Width="175" />
                                </StackPanel>
                            </Button>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        lstDevice.ItemsSource = MainPage.user.dArray.ToList();            
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        var nameInTag=b.Tag.ToString();
    }

已加载专用void PhoneApplicationPage_(对象发送方,路由目标)
{
lstDevice.ItemsSource=MainPage.user.dArray.ToList();
}
private void on按钮单击(对象发送方,路由目标)
{
按钮b=(按钮)发送器;
var nameInTag=b.Tag.ToString();
}
对xaml和cs代码进行如下更改:
已加载专用void PhoneApplicationPage_(对象发送方,路由目标)
{
lstDevice.ItemsSource=MainPage.user.dArray.ToList();
}
私有无效项_选择(对象发送者,选择ChangedEventArgs e)
{
var selctedItem=lstDevice.SelectedItem as(列表框的itmsource)
}

错误:无法识别或无法访问成员“SelectionChange”。现在我的dArray具有我已在OnButton中提供给lstDevice.ItemSource的信息单击方法我想识别单击了dArray的哪个元素。你知道怎么做吗?dArray是什么?我将使用类似
var c=dArray.Where(x=>x==nameInTag)的lambda表达式实际上我有一个类设备,dArray是数组中的一个,例如Device[]dArray;设备有名称、id、序列号等字符串信息,我想在屏幕上显示为按钮,通过单击我想获得该设备的名称、id、序列号等信息。我的意思是,我如何将dArray[0]和dArray[1]的信息与相应的按钮绑定?DataContext应该处理这个问题,设置按钮的标记
tag=“{Binding id}”
和您拥有的OnClick事件
var c=dArray.Where(x=>x.ID.ToString()==nameInTag)。选择(x=>x).FirstOrDefault()
<Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto">
            <ListBox x:Name="lstDevice">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel/>                        
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate >
                            <Button x:Name="btnData" Click="OnButtonClick" Tag="{Binding Name}" >
                                <StackPanel Orientation="Vertical">
                                    <Canvas 
                                            Width="175" 
                                            Height="175"/>                                            
                                    <TextBlock Text="{Binding Name}" Width="175" />
                                </StackPanel>
                            </Button>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        lstDevice.ItemsSource = MainPage.user.dArray.ToList();            
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        var nameInTag=b.Tag.ToString();
    }
Make change in your xaml and cs code like this:

 <Grid x:Name="ContentPanel" Grid.Row="1" Height="Auto">
                <ListBox x:Name="lstDevice" SelectionChange="item_Select">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel/>                        
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate >
                            <StackPanel>
                                <Button x:Name="btnData" >
                                    <StackPanel Orientation="Vertical">
                                        <Canvas 
                                                Width="175" 
                                                Height="175"/>                                            
                                        <TextBlock Text="{Binding Name}" Width="175" />
                                    </StackPanel>
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>


        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {
                lstDevice.ItemsSource = MainPage.user.dArray.ToList();            

            }

            private void item_Select(object sender, SelectionChangedEventArgs e)
            {
                var selctedItem  = lstDevice.SelectedItem as (Your list box's itmsource)
            }