0

当用户可以输入他们的体重时,我有一个页面。数据始终存储为 lbs,因为我在应用程序中的大部分计算都是基于 lbs。但是,在视觉上,用户可以输入公斤、磅和石头。

因此,对于我的问题的示例,可以说我们有以下课程。

int UnitType
double Weight

现在,可以说我有一个页面,其中有一个 ComboBox,SelectedIndex 绑定到UnitType. ComboBox 有 3 个项目“lbs”、“kg”和“st”。在它下面有一个文本框,您可以输入一个值。这将绑定到Weight并且将有一个转换器。

假设用户选择“lbs”并输入 200,但随后决定从 lbs 更改为 kg。我的问题是如何让 ComboBox 的部分更改事件触发文本框转换器的刷新?

在我的脑海中,我猜我将不得不做这样的事情

private int _unitType;
public int UnitType
{
   get { return _unitType;}
   set 
   { 
     _unitType = value;
     NotifyPropertyChanged("UnitType");
     NotifyPropertyChanged("Weight");
   }
 }

private double _weight;
public double Weight
{
   get { return _weight;}
   set 
   { 
     _weight= value;
     NotifyPropertyChanged("Weight");
   }
 }

这会起作用还是我可以做其他事情,也许在绑定本身?

4

2 回答 2

2


下面给出了一种可能的实现方式: 这里我们使用一个因子变量来存储权重的相对值。我们将相对值存储在构造函数中。
湾。1 磅 = 0.453592 公斤 = 0.0714286f 石头。
C。重量的转换是在 Selected Weight Type 中设置属性完成的

            private WeightType _selectedWeightType;
            public WeightType SelectedWeightType
            {
                get { return _selectedWeightType; }
                set
                {
                    var previousType = _selectedWeightType;
                    _selectedWeightType = value;
                    NotifyPropertyChanged("SelectedWeightType");
                    if(previousType != null)
                        CurrentWeight = (CurrentWeight / previousType.Factor) * _selectedWeightType.Factor;
                }
            }

完整代码如下:
XAML:

<Window x:Class="TestWPFApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWPFApp"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <StackPanel Orientation="Vertical" VerticalAlignment="Center">
            <ComboBox Width="100" Height="20" ItemsSource="{Binding WeightTypeList}" DisplayMemberPath="UnitType" SelectedIndex="0" SelectedItem="{Binding SelectedWeightType}">
            </ComboBox>
            <TextBox Width="100" Height="20" Text="{Binding CurrentWeight,Mode=TwoWay}"></TextBox>
        </StackPanel>
    </Grid>
</Window>

代码背后:

using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System;
using System.ComponentModel;
using System.Windows.Data;
using System.Globalization;
namespace TestWPFApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainViewModel();
        }
    }

    public class WeightType : INotifyPropertyChanged
    {
        private int _id;
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                NotifyPropertyChanged("Id");
            }
        }

        private string _unitType;
        public string UnitType
        {
            get { return _unitType; }
            set
            {
                _unitType = value;
                NotifyPropertyChanged("UnitType");
            }
        }

        private float _factor;
        public float Factor
        {
            get { return _factor; }
            set
            {
                _factor = value;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// Call this to force the UI to refresh it's bindings.
        /// </summary>
        /// <param name="name"></param>
        public void NotifyPropertyChanged(String name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public class MainViewModel : INotifyPropertyChanged
    {
        private List<WeightType> _weightTypeList = new List<WeightType>();
        public List<WeightType> WeightTypeList
        {
            get { return _weightTypeList; }
            set
            {
                _weightTypeList = value;
            }
        }
        private double _currentWeight;
        public double CurrentWeight
        {

            get { return _currentWeight; }
            set
            {
                _currentWeight = value;
                NotifyPropertyChanged("CurrentWeight");
            }
        }

        private WeightType _selectedWeightType;
        public WeightType SelectedWeightType
        {
            get { return _selectedWeightType; }
            set
            {
                var previousType = _selectedWeightType;
                _selectedWeightType = value;
                NotifyPropertyChanged("SelectedWeightType");
                if(previousType != null)
                    CurrentWeight = (CurrentWeight / previousType.Factor) * _selectedWeightType.Factor;
            }
        }

        public MainViewModel()
        {
            WeightTypeList.Add(new WeightType() { Id = 1, UnitType = "Lbs", Factor = 1f });
            WeightTypeList.Add(new WeightType() { Id = 2, UnitType = "Kg",Factor = 0.453592f });
            WeightTypeList.Add(new WeightType() { Id = 1, UnitType = "St", Factor = 0.0714286f });
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return null;
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture)
        {
            return null;
        }
    }

}
于 2013-05-22T10:39:17.807 回答
1
  • 您可以拥有一个计算属性,该属性知道如何在所选系统中转换和显示重量。您将在 UnitType 和 Weight 的两组上调用 NotifyPropertyChanged("CalculatedWeight")。

  • 或者,我会使用你的方法。在 Weight 属性的绑定中有一个转换器并将逻辑放在那里。

  • 更好的选择是创建一个具有组合框和文本框的用户控件,创建一个类“WeightProvider”并将 userControl 数据上下文绑定到该类,然后您有两个选择:

    1. 该类具有计算的属性
    2. 用户控制有转换器本身

    如果您要拥有更多带有权重的页面,这是最有效的方法。

于 2013-05-22T10:01:42.147 回答