使用两个选定的组合框项目计算文本框 [英] Calculating textbox with two selected combobox items

查看:145
本文介绍了使用两个选定的组合框项目计算文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:Id喜欢,选择两个组合框变量后,将这两个变量分开,并将Textbox设置为计算结果。

My problem: Id like to , after two combobox variables are selected, to divide these two and set the Textbox to the result of the calculation.

两个Combobox :Körpergröße& Gewicht

The two Comboboxes: Körpergröße & Gewicht

文本框:BMI

首先,代码im using(显然现在不工作)

First of all, the code im using ( which apparently isnt working now)

    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)

The default values of the two comboboxes are strings.. ("Bitte auswählen")

图片现在看起来像。选择两个int值后,计算结果应显示在BMI文本框中,但其仍为空。它看起来像ToString()方法不保存到一个,而不是b。因此,这些值不能用于fillTextBox方法

Pictures how it looks like now. After two int values are selected, the result of the calculation should appear in the BMI Textbox, but its still blank. it looks like the ToString() Methods do not save into a, and neither into b..therefore the values cant be used in the fillTextBox method

如果有人能用一些代码回答我,这对我来说很好,为了让我理解..

It would be nice if someone could answer me with a code with some sidenotes, in order for me to understand..

提前感谢!

推荐答案

这里有一个例子, BMI计算器在WPF。这是XAML打算使用的方式:所有的逻辑都在View Model类BMIViewModel中。视图(XAML文件)围绕该逻辑环绕一个UI。仅当您需要提供视图本身特有的一些特殊逻辑时,才使用codebehind文件。很多时候它根本不使用。这里,它不使用。

Here's an example of how you would write a trivial BMI calculator in WPF. This is the way XAML is intended to be used: All of the logic is in the View Model class, BMIViewModel. The View (XAML file) wraps a UI around that logic. The codebehind file is only used if you need to provide some special logic unique to the view itself. Very often it is not used at all. Here, it's not used.

这与你可能习惯的非常不同,这是一个陡峭的学习曲线在很多方面,但我学会了非常喜欢。如果你在各种视图模型中将程序逻辑分解为合理的块,你可以以不同的方式在UI中呈现这些视图模型。你获得巨大的自由和力量。一旦你有一个调试和测试的View Model,你可以为它编写新的UI,而不触摸程序逻辑。

This is very different from what you may be used to and it's a steep learning curve in a lot of ways, but I've learned to like it very much. If you break up program logic into sensible chunks in various View Models, you can present those View Models in the UI in various differing ways. You get tremendous freedom and power. Once you've got a debugged and tested View Model, you can write new UI for it without touching the program logic at all.

如果你学习并理解这段代码,你将有一个初步但坚实的基础来开始学习XAML。绑定很重要,OnPropertyChanged非常重要:该通知是视图模型如何让绑定知道值已更改。

If you study and understand this code, you will have a rudimentary but solid basis to start learning XAML. The bindings are important, and OnPropertyChanged is very important: That notification is how the view model lets the bindings know that a value has changed.

我不喜欢为初学者写这么多的代码,但是你的代码(这不是你的代码 - 它完全从错误的答案复制到此前的迭代问题),是不可用的。

I don't like writing this much code for a beginner, but your code (which is not your code -- it's entirely copied from bad answers to previous iterations of this question), is unusable.

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#代码后面(我没有添加任何代码):

C# code behind (I added no code at all here):

using System;
using System.Windows;

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

BMIViewModel.cs:

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
    }
}

这篇关于使用两个选定的组合框项目计算文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆