Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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# 如何从ListPickerFlyout检索项目?_C#_Xaml_Windows Phone 8.1 - Fatal编程技术网

C# 如何从ListPickerFlyout检索项目?

C# 如何从ListPickerFlyout检索项目?,c#,xaml,windows-phone-8.1,C#,Xaml,Windows Phone 8.1,我创建了一个ListPickerLyout,我想通过一个按钮从列表中选择一个项目。 在XAML中,我这样做了: <Button x:Name="BottoneFiltraCittaNotizie" Click="BottoneFiltraCittaNotizie_Click" Style="{StaticResource ButtonSearchStyle}" Grid.Row="0" BorderBrush="{x:Null}" Foreground="Gray" Margin="0,-

我创建了一个ListPickerLyout,我想通过一个按钮从列表中选择一个项目。 在XAML中,我这样做了:

<Button x:Name="BottoneFiltraCittaNotizie" Click="BottoneFiltraCittaNotizie_Click" Style="{StaticResource ButtonSearchStyle}" Grid.Row="0" BorderBrush="{x:Null}" Foreground="Gray" Margin="0,-12,0,0">
    <Button.Flyout>
       <ListPickerFlyout ItemsSource="{Binding Source={StaticResource Museum}}">
              <ListPickerFlyout.ItemTemplate>
                    <DataTemplate>
                          <StackPanel>
                              <TextBlock Text="{Binding NomeProvincia}" HorizontalAlignment="Left"/>
                          </StackPanel>
                    </DataTemplate>
              </ListPickerFlyout.ItemTemplate>
       </ListPickerFlyout>
    </Button.Flyout>
</Button>

将对象的属性添加到视图模型中(或代码隐藏,或任何用作datacontex的对象),然后向该列表中的属性添加绑定

假设您在datacontext中有
public MyObject my_object{get;set;}
,您的xaml上应该有如下内容:

<ListPickerFlayout ...
                   SelectedItem = {binding my_object;} />
或者,正如本文所建议的,您不必绑定到属性(我会绑定,因为我将尝试以MVVM的方式进行绑定),您所要做的就是转换发送方并使用它的选定项,如下所示:

void PrintText(object sender, SelectionChangedEventArgs args)
{
    // get your object with the cast, and then get it's item
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    // then you can use it like:
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}

我试过了,但就像我在代码后面做的那样?没有属性“listpickerflyot.SelectedItem”,只有“listpickerflyot.SelectedItemProperty”我更新了答案。另外,请看一下我的文章,它可能会让您对所选项目以及您可以使用它做什么有更多的了解:)
public class SomeClass {
    // this is your code behind file
    public MyObject my_object {get;set;} 

    // this is where you go when you hit the button
    OnButtonClick(sender, event) {
        //my_object is accessible here. Assuming it has a DoSomething method, you can:
        my_object.DoSomething();
    }
}
void PrintText(object sender, SelectionChangedEventArgs args)
{
    // get your object with the cast, and then get it's item
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    // then you can use it like:
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}