【问题标题】:wrappanel listbox item select eventwrappanel 列表框项目选择事件
【发布时间】:2014-01-09 11:34:04
【问题描述】:

我使用 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>

上面是设计代码,下面是c#代码

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

        private void item_Select(object sender, SelectionChangedEventArgs e)
        {
            int p = ((ListBox)sender).SelectedIndex;
        }

如何生成列表框项目选择事件并获取数字或某些属性以识别选择了哪个项目?提前致谢!

【问题讨论】:

    标签: c# silverlight windows-phone-7 listbox wrappanel


    【解决方案1】:

    我认为这可能更适合你:

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

    【讨论】:

    • 现在我的 dArray 有我在 OnButtonClick 方法中提供给 lstDevice.ItemSource 的信息,我想识别点击了 dArray 的哪个元素。你知道怎么做吗?
    • 什么是 dArray?我会使用像 var c = dArray.Where(x =&gt; x == nameInTag).Select(x =&gt; x).FirstOrDefault(); 这样的 lambda 表达式
    • 实际上我有一个类 Device ,其中 dArray 是数组之一,例如设备[] dArray;设备具有字符串中的信息,如名称、id、序列号等,我想将其显示为屏幕上的按钮,通过单击我想获取该设备的名称、id、序列号等信息。
    • 我的意思是如何将 dArray[0],dArray[1] 的信息与相应的按钮绑定?
    • DataContext 应该处理那个,设置按钮的标签Tag="{Binding ID}" 和 OnClick 事件你有var c = dArray.Where(x =&gt; x.ID.ToString() == nameInTag).Select(x =&gt; x).FirstOrDefault();
    【解决方案2】:
    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)
                }
    

    【讨论】:

    • 错误:成员“SelectionChange”无法识别或无法访问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    相关资源
    最近更新 更多