全屏模式下的Listpicker点击事件无法正常工作

时间:2014-04-22 06:46:13

标签: c# xaml windows-phone-8 listpicker

我正在开发Windows Phone 8应用程序。

我正在使用ListPicker选项。

我的Listpicker代码

<toolkit:ListPicker x:Name="LPfilter" Foreground="White" BorderThickness="0"  Margin="300,0,0,0"  Height="80" Width="50" Visibility="Visible">
            <toolkit:ListPicker.Background>
                <ImageBrush ImageSource="/Assets/Images/filters.png"/>
            </toolkit:ListPicker.Background>

            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding name}"   Visibility="Collapsed" Foreground="Red"/>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>

            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <TextBlock Tap="TextBlock_Tap">
                        <Run Text="{Binding name}"/>
                    </TextBlock>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>

使用webservice(json格式)将值绑定到listpicker。

Json结果就像这样

[
  -{
     id: "9",
     name: "Pizza",
     root_id: "4",
     level: "1",
   },

  -{
     id: "10",
     name: "Fine Dinind",
     root_id: "4",
     level: "1",
   },

  -{
      id: "11",
      name: "Fast Food",
      root_id: "4",
      level: "1",
   },
    ....
]

c#绑定值代码

public void businesscatbind()
    {

            string bus_caturl = "http://xxxxx.com/Service/filterquery.php?rootid=" + bus_catval;
            WebClient bus_catwc = new WebClient();

            bus_catwc.DownloadStringAsync(new Uri(bus_caturl), UriKind.Relative);
            bus_catwc.DownloadStringCompleted += bus_catwc_DownloadStringCompleted;
    }

 void bus_catwc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        var bus_catdata = e.Result;
        var bus_catvalue = JsonConvert.DeserializeObject<List<bus_catbinddata>>(bus_catdata);
        LPfilter.ItemsSource = bus_catvalue;
     }

此事件中出现问题

    private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        bus_catbinddata elements = LPfilter.SelectedItem as bus_catbinddata;
        int val = LPfilter.SelectedIndex;

        filterid = int.Parse(elements.id);
        MessageBox.Show(filterid.ToString());
     }

我的问题

首次点击披萨,它会在msgbox

中显示值= Id值9

接下来我点击Fine Dining它显示msgbox中的值= Id值9

接下来我点击快餐它在msgbox中显示值= 10(10是FineDining的idvalue)

接下来我点击意大利语,它在msgbox中显示值= 11(11是快餐的Idvalue)

预览所选项目值显示在提醒

输出

enter image description here

enter image description here

enter image description here

enter image description here

如何解决这个问题。

4 个答案:

答案 0 :(得分:0)

使用Listpicker SelectionChanged事件代替

<toolkit:ListPicker x:Name="LPfilter" Foreground="White" BorderThickness="0"  Margin="300,0,0,0"  Height="80" Width="50" Visibility="Visible" SelectionChanged="listPicker_SelectionChanged>

答案 1 :(得分:0)

似乎&#34; TextBlock_Tap&#34;事件在&#34; ListPicker&#34;之前触发改变其选择。

您可以尝试使用&#34; SelectionChanged&#34; ListPicker的事件 - 虽然您似乎没有使用它,因为您可能想要两次选择相同的选项。在这种情况下,您可以在列表顶部添加一个虚拟项目,例如&#34;选择项目&#34;并且在&#34; SelectionChanged&#34;最后的事件将列表选择器的选定项目设置为您的虚拟项目。

编辑:

在这种方法中 -

void bus_catwc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    var bus_catdata = e.Result;
    var bus_catvalue = JsonConvert.DeserializeObject<List<bus_catbinddata>>(bus_catdata);
    LPfilter.ItemsSource = bus_catvalue;
 }

添加一个新的&#34; bus_catbinddata&#34;到&#34; var bus_catvalue&#34; - 它是一个列表 - 在索引0处插入您的虚拟itme。然后在列表选择器的选择更改事件中添加条件if(ListPicker.SelectedIndex&gt; 0)并且仅当此条件为真时,代码才会执行​​。

答案 2 :(得分:0)

在Xaml页面

<toolkit:ListPicker x:Name="listPicker" Foreground="White" BorderThickness="0"  Margin="300,0,0,0"  Height="80" Width="50" Visibility="Visible" SelectionChanged="listPicker_SelectionChanged" >
        <toolkit:ListPicker.Background>
            <ImageBrush ImageSource="/Assets/Images/filters.png"/>
        </toolkit:ListPicker.Background>

        <toolkit:ListPicker.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding name}"   Visibility="Collapsed" Foreground="Red"/>
            </DataTemplate>
        </toolkit:ListPicker.ItemTemplate>

        <toolkit:ListPicker.FullModeItemTemplate>
            <DataTemplate>
                <TextBlock Tap="TextBlock_Tap">
                    <Run Text="{Binding name}"/>
                </TextBlock>
            </DataTemplate>
        </toolkit:ListPicker.FullModeItemTemplate>
    </toolkit:ListPicker>

在.cs文件中

 void listPicker_SelectionChanged(object sender, SelectionChangedEventArgs args)
    {

        if (this.listPicker.SelectedItems != null && this.listPicker.SelectionMode == SelectionMode.Multiple)
        {
            for (int i = 0; i < this.listPicker.SelectedItems.Count; i++)
            {
                string str = ((Items)(this.listPicker.SelectedItems[i])).Name;
                if (i == 0)
                {
                   MessageBox.Show("Selected Item(s) is " + str);
                }
                else
                {
                    //Some Code
                }
            }
        }
        else if (this.listPicker.SelectionMode == SelectionMode.Single)
        {
            MessageBox.Show("Selected Item is " + ((Items)this.listPicker.SelectedItem).Name);
        }
    }

答案 3 :(得分:0)

我还没有专门针对ListPicker的完整模式项模板尝试此操作,但您可以尝试从TextBlock获取相应的模型以获取DataContext

private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    TextBlock textBlock = (TextBlock)sender;
    bus_catbinddata elements = textBlock.DataContext as bus_catbinddata;

    filterid = int.Parse(elements.id);
    MessageBox.Show(filterid.ToString());
 }