Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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# 如何通过DataContext从视图中获取点击项_C#_Windows Phone 7_Windows Phone 8_Datacontext_Listpicker - Fatal编程技术网

C# 如何通过DataContext从视图中获取点击项

C# 如何通过DataContext从视图中获取点击项,c#,windows-phone-7,windows-phone-8,datacontext,listpicker,C#,Windows Phone 7,Windows Phone 8,Datacontext,Listpicker,这对我来说是一个有趣的问题,我的视图中有一个ListPicker,但在ListPicker的DataTemplate中使用了一个StackPanel,因此我可以避免在页面导航到时调用ListPickerSelectionChanged事件的问题。在这个实现中,我很难在视图中获取ListPicker中选择的项。我需要获取ListPicker中所选项目的name(亮或暗)的小写版本 MainPage.xaml <phone:PhoneApplicationPage.Resources>

这对我来说是一个有趣的问题,我的视图中有一个
ListPicker
,但在
ListPicker
DataTemplate
中使用了一个
StackPanel
,因此我可以避免在页面导航到时调用
ListPicker
SelectionChanged
事件的问题。在这个实现中,我很难在视图中获取ListPicker中选择的项。我需要获取ListPicker中所选项目的
name
(亮或暗)的小写版本

MainPage.xaml

<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="PickerItemTemplate"> 
       <StackPanel tap="stk_Tap"> 
            <TextBlock Text="{Binding Name}"/> 
        </StackPanel> 
</DataTemplate>
</phone:PhoneApplicationPage.Resources>

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme"
            ItemTemplate="{StaticResource PickerItemTemplate}"/>

MainPage.xaml.cs

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    themeList = new List<TestApp.Common.Theme>();
    themeList.Add(new TestApp.Common.Theme() { Name = "Darker", name = "dark" });
    themeList.Add(new TestApp.Common.Theme() { Name = "Lighter", name = "light" });
    ThemeListPicker.ItemsSource = themeList;
}

private void stk_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (ThemeListPicker.SelectedIndex != -1)
    {
        //Need to get the current ThemeListPicker's 'name'
        var selectedItem1 = (sender as StackPanel).DataContext as ListPicker;

        //use selectedItem1

    }
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
themeList=新列表();
添加(newtestapp.Common.Theme(){Name=“Darker”,Name=“dark”});
添加(newtestapp.Common.Theme(){Name=“Lighter”,Name=“light”});
themelisticker.ItemsSource=themeList;
}
私有void stk_Tap(对象发送方,System.Windows.Input.GestureEventArgs e)
{
如果(themelisticker.SelectedIndex!=-1)
{
//需要获取当前Themelisticker的“名称”
var selectedItem1=(发送方作为StackPanel),DataContext作为ListPicker;
//使用selectedItem1
}
}

该堆栈面板的数据上下文是TestApp.Common.Theme

所以,使用这个应该是可行的

var selectedItem1 = (sender as StackPanel).DataContext as TestApp.Common.Theme;