两个ListPicker,一个TextBox。结果未显示。主详细?

时间:2015-09-15 15:23:26

标签: c# if-statement windows-phone-8 master-detail listpicker

我有两个源自一个TextBox的ListPickers。

    private void textBox3_GotFocus(object sender, RoutedEventArgs e)
    {
        string a = "Domestic";
        string b = Convert.ToString(textBox2.Text);
        string c = "Foreign";

        if (b == a)
        {
            NavigationService.Navigate(new Uri("/ListPickerExchanges1.xaml", UriKind.Relative));
        }
        else if (b == c)
            {
                NavigationService.Navigate(new Uri("/ListPickerExchanges2.xaml", UriKind.Relative));
            }

两个ListPickers都会加载并为您提供从列表中选择内容的选项。但是只有一个ListPicker会将选择显示回到TextBox3中,而它总是在第二个Else If(b == c)条件下的那个。

第一个条件下的ListPickerExchanges1不会将选择显示回textbox3。

如果我更改第二个Else If条件下的代码以导航到Exchanges1而不是Exchanges2,那么Exchanges1 listpicker会将选择显示回文本框3,而Exchanges2则不会。

这意味着,第二个条件下的所有内容都有效,并且不在第一个条件下

以下是ListpickerExchanges1背后的代码,用于将选择内容显示回文本框。

          public partial class ListPickerExchanges1 : PhoneApplicationPage
{
    public ListPickerExchanges1()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(ListPickerExchanges1_Loaded);
    }
    private void ListPickerExchanges1_Loaded(object sender, RoutedEventArgse)
    {
        ListBoxExchanges.ItemsSource = Page2.ListExchanges;
    }
      public void Selection(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ListBox myselecteditem4 = (ListBox)sender;
        ItemsModeling4 item4 = (ItemsModeling4)myselecteditem4.SelectedItem;            
        if (item4 != null)
        {
            NavigationService.Navigate(new Uri("/Broker.xaml?name4=" + item4.Exchanges, UriKind.RelativeOrAbsolute));
        }
         }

............................................... .................................................. .................................................. ........... 更新:导航和选择背后的附加代码

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        string name4 = "";
        NavigationContext.QueryString.TryGetValue("name4", out name4);
        textBox3.Text = string.Format("{0}", name4);
    }        

............................................... .................................................. .................................................. ...........

    public Page2()
    {
        InitializeComponent();
        ListExchanges = new ObservableCollection<ItemsModeling4>();
        this.Loaded += new RoutedEventHandler(Page44_Loaded);
    }

............................................... .................................................. .................................................. ...........

    private void Page44_Loaded(object sender, RoutedEventArgs e)
    {
        if (ListExchanges.Count == 0)
        {
            string[] arrangeExchanges = new string[] { "Option1", "Option2", "Option3", "Option4" };
            foreach (string item4 in arrangeExchanges)
            {
                ListExchanges.Add(new ItemsModeling4 { Exchanges = item4 });
            }
        }
    }
    public static ObservableCollection<ItemsModeling4> ListExchanges { get; private set; }

............................................... .................................................. .................................................. ...........

public class ItemsModeling4
{
    public string Exchanges { get; set; }
}

............................................... .................................................. .................................................. ........... XAML

       <ListBox x:Name="ListBoxExchanges" Background="Transparent" 
         Tap="Selection">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Height="Auto" FontFamily="Segoe WP Semibold" FontSize="50" HorizontalAlignment="Left" Margin="12,0,0,0"
                Text="{Binding Exchanges}" VerticalAlignment="Top" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我在这里做错了什么?

它看起来像一个大师 - 细节绑定问题。我只是不知道在哪里或如何解决它。

系统没有给出任何错误,我已经用调试器检查了一切,一切似乎都很好。 listpickers独立工作,我不知道代码可能会介入的位置,并阻止其工作。这一切对我来说都很好。 帮助

1 个答案:

答案 0 :(得分:2)

  

但是,如果我更改第二个Else If条件下的代码以导航到Exchanges1而不是Exchanges2,那么Exchanges1 listpicker会将选择显示回文本框3,而Exchanges2则不会。

您确定要比较文化正确的字符串吗?请尝试这种比较,而不是两者。

if (String.Equals(a, b, InvariantCultureIgnoreCase))

有关详细信息,请参阅StringComparison Enumeration (System)Best Practices for Using Strings in the .NET Framework

否则字符串对于空格来说可能很棘手。尝试

String.Equals(a.Trim(), b.Trim());

删除用户可能添加\r\n的任何空格?

坦率地说,因为代码知道选择,所以最好只为ComboBox提供用户可选择的选项,而不是使用TextBox