2016-05-03 11 views
0

Dies ist eine Fortsetzung von einem earlier question. Ich versuche, durch die Bindung Combobox schließlich zu arbeiten, damit ich es für einige Einstellungen verwenden kann. danach möchte ich den Inhalt der Textbox mit einer Combobox-Auswahl kombinieren, die auf der Schaltfläche ausgeführt werden soll. Wie behalte ich die Combobox-Auswahl, die bereits aktiv ausführbar ist?UWP halten Combobox-Auswahl, die bereits aktiv ausführbar ist

private void button_Click(object sender, RoutedEventArgs e) 
     { 
      String satuan = ComboBox.GetIsSelectionActive(combobox).ToString(); 
       if (satuan.Equals("0")) 
       { 
        awal = Convert.ToDouble(textbox.Text); 
        celcius = awal; 
        textbox1.Text = celcius.ToString(); 
        reamur = 0.8 * awal; 
        textbox2.Text = reamur.ToString(); 
        fahrenheit = (1.8 * awal) + 32; 
        textbox3.Text = fahrenheit.ToString(); 
        kelvin = awal + 273; 
        textbox4.Text = kelvin.ToString(); 
       } 

Dies ist die XAML:

<Page 
    x:Class="berhasil.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:berhasil" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:converter="using:berhasil.Converter" 
    mc:Ignorable="d"> 

    <Page.Resources> 
     <converter:ConverterString x:Key="Converter" /> 
    </Page.Resources> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="Auto"/> 
     </Grid.ColumnDefinitions> 

     <TextBlock Grid.Row="0" Text="Input Value: " VerticalAlignment="Center"/> 
     <TextBox x:Name="textbox" 
       Grid.Row="0" 
       Grid.Column="1" 
       Margin="0,10,0.333,0" 
       Width="154" 
       VerticalAlignment="Center" 
       InputScope="Number"/> 
     <StackPanel Grid.Column="2" 
        Grid.Row="0" 
        VerticalAlignment="Center" 
        Margin="20,10,0,10" 
        Orientation="Horizontal"> 
      <ComboBox x:Name="combobox" 
         ItemsSource="{x:Bind ComboBoxOptions}" 
         SelectedItem="{x:Bind SelectedComboBoxOption, Mode=TwoWay, Converter={StaticResource Converter}}" 
         SelectedValuePath="ComboBoxOption" 
         DisplayMemberPath="ComboBoxOption"> 
      </ComboBox> 
     </StackPanel> 
     <Button Grid.Row="1" x:Name="button" 
       Content="Conversion" 
       HorizontalAlignment="Center" Margin="78,0,0.333,0" 
       Width="154" Grid.ColumnSpan="2" 
       Click="button_Click"/> 
     <TextBlock Grid.Row="2" Text="Celcius: " VerticalAlignment="Center"/> 
     <TextBox x:Name="textbox1" 
       Grid.Row="2" 
       Margin="78,10,-23.333,0" 
       Grid.ColumnSpan="3" 
       VerticalAlignment="Center" 
       InputScope="Number" 
       IsReadOnly="True"/> 
     <TextBlock Grid.Row="3" Text="Reamur: " VerticalAlignment="Center"/> 
     <TextBox x:Name="textbox2" 
       Grid.Row="3" 
       Margin="78,10,-23.333,0" 
       Grid.ColumnSpan="3" 
       VerticalAlignment="Center" 
       InputScope="Number" 
       IsReadOnly="True"/> 
     <TextBlock Grid.Row="4" Text="Fahrenheit: " VerticalAlignment="Center"/> 
     <TextBox x:Name="textbox3" 
       Grid.Row="4" 
       Margin="78,10,-23.333,0" 
       Grid.ColumnSpan="3" 
       VerticalAlignment="Center" 
       InputScope="Number" 
       IsReadOnly="True"/> 
     <TextBlock Grid.Row="5" Text="Kelvin: " VerticalAlignment="Center"/> 
     <TextBox x:Name="textbox4" 
       Grid.Row="5" 
       Margin="78,10,-23.333,0" 
       Grid.ColumnSpan="3" 
       VerticalAlignment="Center" 
       InputScope="Number" 
       IsReadOnly="True"/> 
    </Grid> 
</Page> 

Und das ist der Code hinter:

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using System.Xml.Serialization; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.Storage; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace berhasil 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MainPage : Page, INotifyPropertyChanged 
    { 
     Double awal, celcius, reamur, fahrenheit, kelvin; 
     private ObservableCollection<ComboBoxItem> ComboBoxOptions; 
     ApplicationDataContainer roamingSettings = null; 


     public MainPage() 
     { 
      this.InitializeComponent(); 
      ComboBoxOptions = new ObservableCollection<ComboBoxItem>(); 
      ComboBoxOptionsManager.GetComboBoxList(ComboBoxOptions); 
      roamingSettings = ApplicationData.Current.RoamingSettings; 

      var value = (string)roamingSettings.Values["ComboBoxSelection"]; 
      if (value != null) 
      { 
       var deserialized = Deserialize<ComboBoxItem>(value); 
       // using ComboBoxOption as the primary key field of your object 
       SelectedComboBoxOption = ComboBoxOptions.SingleOrDefault(c => 
          c.ComboBoxOption == deserialized.ComboBoxOption); 
      } 
      else 
      { 
       SelectedComboBoxOption = ComboBoxOptions[0]; 
      } 
     } 

     public class ComboBoxItem 
     { 
      public int ComboBoxId { get; set; } 
      public string ComboBoxOption { get; set; } 

     } 

     public class ComboBoxOptionsManager 
     { 
      public static void GetComboBoxList(ObservableCollection<ComboBoxItem> ComboBoxItems) 
      { 
       var allItems = getComboBoxItems(); 
       ComboBoxItems.Clear(); 
       allItems.ForEach(p => ComboBoxItems.Add(p)); 
      } 

      private static List<ComboBoxItem> getComboBoxItems() 
      { 
       var items = new List<ComboBoxItem>(); 

       items.Add(new ComboBoxItem() { ComboBoxId = 1, ComboBoxOption = "Celcius"}); 
       items.Add(new ComboBoxItem() { ComboBoxId = 2, ComboBoxOption = "Reamur" }); 
       items.Add(new ComboBoxItem() { ComboBoxId = 3, ComboBoxOption = "Fahrenheit" }); 
       items.Add(new ComboBoxItem() { ComboBoxId = 4, ComboBoxOption = "Kelvin" }); 

       return items; 
      } 
     } 

     private ComboBoxItem _SelectedComboBoxOption; 
     public ComboBoxItem SelectedComboBoxOption 
     { 
      get 
      { 
       return _SelectedComboBoxOption; 
      } 
      set 
      { 
       if (_SelectedComboBoxOption != value) 
       { 
        _SelectedComboBoxOption = value; 
        roamingSettings.Values["ComboBoxSelection"] = Serialize(value); 
        RaisePropertyChanged("SelectedComboBoxOption"); 
       } 
      } 
     } 

     public static string Serialize(object obj) 
     { 
      using (var sw = new StringWriter()) 
      { 
       var serializer = new XmlSerializer(obj.GetType()); 
       serializer.Serialize(sw, obj); 
       return sw.ToString(); 
      } 
     } 

     public static T Deserialize<T>(string xml) 
     { 
      using (var sw = new StringReader(xml)) 
      { 
       var serializer = new XmlSerializer(typeof(T)); 
       return (T)serializer.Deserialize(sw); 
      } 
     } 

     void RaisePropertyChanged(string prop) 
     { 
      if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 



     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      String satuan = ComboBox.GetIsSelectionActive(combobox).ToString(); 
       if (satuan.Equals("0")) 
       { 
        awal = Convert.ToDouble(textbox.Text); 
        celcius = awal; 
        textbox1.Text = celcius.ToString(); 
        reamur = 0.8 * awal; 
        textbox2.Text = reamur.ToString(); 
        fahrenheit = (1.8 * awal) + 32; 
        textbox3.Text = fahrenheit.ToString(); 
        kelvin = awal + 273; 
        textbox4.Text = kelvin.ToString(); 
       } 
       else if (satuan.Equals("1")) 
       { 
        awal = Convert.ToDouble(textbox.Text); 
        celcius = 1.25 * awal; 
        textbox1.Text = celcius.ToString(); 
        reamur = awal; 
        textbox2.Text = reamur.ToString(); 
        fahrenheit = (2.25 * awal) + 32; 
        textbox3.Text = fahrenheit.ToString(); 
        kelvin = celcius + 273; 
        textbox4.Text = kelvin.ToString(); 
       } 
       else if (satuan.Equals("2")) 
       { 
        awal = Convert.ToDouble(textbox.Text); 
        celcius = 0.55555 * (awal - 32); 
        textbox1.Text = celcius.ToString(); 
        reamur = 0.44444 * (awal - 32); 
        textbox2.Text = reamur.ToString(); 
        fahrenheit = awal; 
        textbox3.Text = fahrenheit.ToString(); 
        kelvin = celcius + 273; 
        textbox4.Text = kelvin.ToString(); 
       } 
       else if (satuan.Equals("3")) 
       { 
        awal = Convert.ToDouble(textbox.Text); 
        celcius = awal - 273; 
        textbox1.Text = celcius.ToString(); 
        reamur = 0.8 * (awal - 273); 
        textbox2.Text = reamur.ToString(); 
        fahrenheit = (1.8 * (awal - 273)) + 32; 
        textbox3.Text = fahrenheit.ToString(); 
        kelvin = awal; 
        textbox4.Text = kelvin.ToString(); 
       } 
     } 
    } 
} 

Antwort

0

Es scheint, gibt es ein Problem mit:

String satuan = ComboBox.GetIsSelectionActive (Combobox) .ToString();

Die Methode Selector.GetIsSelectionActive ruft einen Wert ab, der angibt, ob der angegebene Selektor den Fokus hat. Und es gibt den Wert Bool-Typ zurück. Die Selector.GetIsSelectionActive gibt immer false zurück. Weil es den Fokus verloren hat, wenn Sie auf die Schaltfläche klicken. So gibt die satuan.Equals("0") immer auch falsch zurück.

Sie können Selector.SelectedIndex verwenden, um den Index des ausgewählten Elements zu erhalten. Dann können Sie den Index verwenden, um die Zahlen zu erfassen.

Zum Beispiel:

private void button_Click(object sender, RoutedEventArgs e) 
{ 
    string satuan=combobox.SelectedIndex.ToString(); 
    switch (satuan) 
    { 
     case "0": 
      //To do 
      break; 
     case "1": 
      //To do 
      break; 
     case "2": 
      //To do 
      break; 
     case "3": 
      //To do 
      break; 
    } 
} 
+0

vielen sir für Ihre Antwort danken –

Verwandte Themen