2015-12-11 21 views
0

我的問題:Id想要在選擇兩個組合框變量後將這兩個項目分開並將文本框設置爲計算結果。計算帶有兩個選定組合框項目的文本框

兩個組合框:Körpergröße& Gewicht

文本框:BMI

首先,使用代碼IM(這顯然心不是現在的工作)

private void fillTextBox(float value1, float value2) 
    { 
     BMI.Text = (value1/value2).ToString(); 
    } 

    private void Körpergröße_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     float a; 
     float b; 
     //In this way you can compare the value and if it is possible to convert into an integer. 
     if (float.TryParse(Körpergröße.SelectedItem.ToString(), out a) && float.TryParse(Gewicht.SelectedItem.ToString(), out b)) 
     { 

      fillTextBox(a, b); 
     } 


    } 

    private void Gewicht_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     float a; 
     float b; 
     //In this way you can compare the value and if it is possible to convert into an integer. 
     if (float.TryParse(Körpergröße.SelectedItem.ToString(), out a) && float.TryParse(Gewicht.SelectedItem.ToString(), out b)) 
     { 

      fillTextBox(a, b); 
     } 
    } 

的默認值兩個組合框是字符串..(「Bitteauswählen」)

圖片它現在是什麼樣子。選擇兩個int值後,計算結果應顯示在BMI文本框中,但仍爲空白。它看起來像的ToString()方法不保存到一個,也不成b..therefore不能在fillTextBox方法

enter image description here

這將是很好,如果有人可以用一個回答我使用的值代碼與一些注意事項,以便我明白..

在此先感謝!

+0

第一步將是爲你在'fillTextBox'中設置一個斷點,並確保它被擊中。如果它被擊中,然後檢查「a」和「b」是什麼。如果它沒有被擊中,那麼我們可以確定問題出在'SelectionChanged'事件 – KSdev

+0

我剛剛複製了你的代碼,併爲我填充了'textbox'。您的'combobox'是以編程方式填充的,還是您已將可能的範圍作爲列表的一部分進行分配?我做的一件事是改變了,而不是'SelectionChanged'我使用了'SelectionChangeCommitted',因爲它只在用戶實際調整'combobox'時觸發 – KSdev

回答

1

下面是如何在WPF中編寫簡單的BMI計算器的示例。這是XAML打算使用的方式:所有邏輯都位於View Model類BMIViewModel中。視圖(XAML文件)圍繞該邏輯封裝了一個UI。只有在需要爲視圖本身提供一些特殊的邏輯時,纔會使用代碼隱藏文件。很多時候它根本不被使用。在這裏,它沒有被使用。

這與你可能習慣的很不一樣,它在很多方面都是陡峭的學習曲線,但我學會了非常喜歡它。如果您在各種視圖模型中將程序邏輯分解爲合理的塊,則可以通過各種不同的方式在UI中呈現這些視圖模型。你獲得巨大的自由和權力。一旦你有了一個經過調試和測試的視圖模型,你就可以爲它編寫新的UI,而不必觸摸程序邏輯。

如果你學習和理解這段代碼,你將有一個基礎但堅實的基礎來開始學習XAML。綁定非常重要,OnPropertyChanged非常重要:該通知是視圖模型如何讓綁定知道值已更改。

我不喜歡爲初學者編寫這麼多的代碼,但是你的代碼(這不是你的代碼 - 它完全是從這個問題的前幾次迭代的錯誤答案中複製過來的)不可用。

XAML:

<Window 
    x:Class="TestApplication.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestApplication" 
    Title="MainWindow" Height="350" Width="525" 
    > 
    <Window.DataContext> 
     <local:ViewModel /> 
    </Window.DataContext> 
    <Grid> 
     <StackPanel Orientation="Vertical"> 
      <StackPanel.Resources> 
       <Style x:Key="FieldLabel" TargetType="Label"> 
        <Setter Property="Width" Value="120" /> 
       </Style> 
       <Style TargetType="ComboBox"> 
        <Setter Property="Width" Value="140" /> 
       </Style> 
       <Style TargetType="TextBox"> 
        <Setter Property="Width" Value="140" /> 
       </Style> 
      </StackPanel.Resources> 
      <StackPanel Orientation="Horizontal"> 
       <Label Content="Height" Style="{StaticResource FieldLabel}" /> 
       <ComboBox 
        ItemsSource="{Binding Heights}" 
        DisplayMemberPath="Name" 
        SelectedValuePath="Value" 
        SelectedValue="{Binding Height}" 
        /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Content="Weight" Style="{StaticResource FieldLabel}" /> 
       <ComboBox 
        ItemsSource="{Binding Weights}" 
        DisplayMemberPath="Name" 
        SelectedValuePath="Value" 
        SelectedValue="{Binding Weight}" 
        /> 
      </StackPanel> 
      <StackPanel Orientation="Horizontal"> 
       <Label Content="BMI" Style="{StaticResource FieldLabel}" /> 
       <TextBox IsReadOnly="True" Text="{Binding BMI}" /> 
      </StackPanel> 
     </StackPanel> 
    </Grid> 
</Window> 

C#代碼隱藏(我加一點代碼都在這裏):

using System; 
using System.Windows; 

namespace TestApplication 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

BMIViewModel.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 

namespace TestApplication 
{ 
    public class BMIListItem 
    { 
     public BMIListItem(string name, float value) 
     { 
      Name = name; 
      Value = value; 
     } 
     public BMIListItem(float value) 
     { 
      Name = value.ToString(); 
      Value = value; 
     } 
     public String Name { get; set; } 
     public float Value { get; set; } 
    } 

    public class BMIViewModel : INotifyPropertyChanged 
    { 
     public BMIViewModel() 
     { 
      // Of course you would write loops for these in real life. 
      // You should not need help with that. 
      Heights = new List<BMIListItem> 
      { 
       new BMIListItem("Bitte auswählen", float.NaN), 
       new BMIListItem("Dummy", 0), 
       new BMIListItem(150), 
       new BMIListItem(151), 
       new BMIListItem(152), 
       // etc. 
      }; 
      Weights = new List<BMIListItem> 
      { 
       new BMIListItem("Bitte auswählen", float.NaN), 
       new BMIListItem("Dummy", 0), 
       new BMIListItem(40), 
       new BMIListItem(41), 
       new BMIListItem(42), 
       // etc. 
      }; 
     } 

     public List<BMIListItem> Heights { get; private set; } 
     public List<BMIListItem> Weights { get; private set; } 

     #region BMI Property 
     private float _bmi = 0; 
     public float BMI 
     { 
      get { return _bmi; } 
      set 
      { 
       if (value != _bmi) 
       { 
        _bmi = value; 
        OnPropertyChanged("BMI"); 
       } 
      } 
     } 
     #endregion BMI Property 

     #region Height Property 
     private float _height = float.NaN; 
     public float Height 
     { 
      get { return _height; } 
      set 
      { 
       if (value != _height) 
       { 
        _height = value; 
        UpdateBMI(); 
        OnPropertyChanged("Height"); 
       } 
      } 
     } 
     #endregion Height Property 

     #region Weight Property 
     private float _weight = float.NaN; 
     public float Weight 
     { 
      get { return _weight; } 
      set 
      { 
       if (value != _weight) 
       { 
        _weight = value; 
        UpdateBMI(); 
        OnPropertyChanged("Weight"); 
       } 
      } 
     } 
     #endregion Weight Property 

     private void UpdateBMI() 
     { 
      if (float.IsNaN(Weight) || float.IsNaN(Height) || Height == 0) 
      { 
       BMI = 0; 
      } 
      else 
      { 
       BMI = Weight/Height; 
      } 
     } 

     #region INotifyPropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(String propName) 
     { 
      var handler = PropertyChanged; 
      if (null != handler) 
      { 
       handler(this, new PropertyChangedEventArgs(propName)); 
      } 
     } 
     #endregion INotifyPropertyChanged 
    } 
} 
相關問題