C# 将一个控件上的属性更改为刷新另一个控件上的转换器?
我有一个页面,用户可以输入他们的体重。数据总是以磅的形式存储,因为我在应用程序中的大多数计算都是基于磅的。但是,用户可以在视觉上输入千克、磅和石头 以我的问题为例,假设我们有以下课程C# 将一个控件上的属性更改为刷新另一个控件上的转换器?,c#,wpf,xaml,C#,Wpf,Xaml,我有一个页面,用户可以输入他们的体重。数据总是以磅的形式存储,因为我在应用程序中的大多数计算都是基于磅的。但是,用户可以在视觉上输入千克、磅和石头 以我的问题为例,假设我们有以下课程 int UnitType double Weight 现在,假设我有一个页面,其中有一个组合框,其中SelectedIndex绑定到UnitType。组合框有3个项目“磅”、“千克”和“圣”。下面有一个文本框,您可以输入一个值。这将绑定到重量,并将有一个转换器 假设用户选择“lbs”并输入200,然后决定从lbs
int UnitType
double Weight
现在,假设我有一个页面,其中有一个组合框,其中SelectedIndex绑定到UnitType
。组合框有3个项目“磅”、“千克”和“圣”。下面有一个文本框,您可以输入一个值。这将绑定到重量
,并将有一个转换器
假设用户选择“lbs”并输入200,然后决定从lbs更改为kg。我的问题是如何让ComboBox的section changed事件触发textbox转换器的刷新
在我的脑海里,我猜我必须做这样的事情
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");
}
}
这会起作用吗?或者我还能做些什么,也许是在绑定本身?- 您可以拥有一个计算属性,该属性知道如何在所选系统中转换和显示权重。您可以在UnitType和Weight两组上调用NotifyPropertyChanged(“CalculatedWeight”)
- 或者,我会使用你的方法。在Weight属性的绑定中有一个转换器,并将逻辑放在那里
- 更好的选择是创建一个包含combobox和textbox的用户控件,创建一个类“WeightProvider”,并将userControl datacontext绑定到该类,然后您有两个选择:
- 该类具有计算属性
- 用户控件具有转换器本身 这是最有效的方法,如果你想有更多的网页与重量
- 您可以拥有一个计算属性,该属性知道如何在所选系统中转换和显示权重。您可以在UnitType和Weight两组上调用NotifyPropertyChanged(“CalculatedWeight”)
- 或者,我会使用你的方法。在Weight属性的绑定中有一个转换器,并将逻辑放在那里
- 更好的选择是创建一个包含combobox和textbox的用户控件,创建一个类“WeightProvider”,并将userControl datacontext绑定到该类,然后您有两个选择:
- 该类具有计算属性
- 用户控件具有转换器本身 这是最有效的方法,如果你想有更多的网页与重量
- 下面给出了一种可能的实现方法:
a。这里我们使用一个因子变量来存储权重的相对值。我们将相对值存储在构造函数中。
b。1磅=0.453592千克=0.0714286f石头。
c。权重的转换是在选定权重类型中设置属性完成的
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:
代码隐藏:
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;
}
}
}
使用System.Windows;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
使用制度;
使用系统组件模型;
使用System.Windows.Data;
利用制度全球化;
命名空间TestWPFApp
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=新的MainViewModel();
}
}
公共类权重类型:INotifyPropertyChanged
{
私人内部id;
公共整数Id
{
获取{return\u id;}
设置
{
_id=值;
NotifyPropertyChanged(“Id”);
}
}
私有字符串_unitType;
公共字符串单位类型
{
获取{return\u unitType;}
设置
{
_单位类型=值;
NotifyPropertyChanged(“单位类型”);
}
}
私人浮动系数;
公众浮动系数
{
获取{return\u factor;}
设置
{
_系数=价值;
}
}
公共事件属性更改事件处理程序属性更改;
///
///调用此函数可强制UI刷新其绑定。
///
///
public void NotifyPropertyChanged(字符串名称)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(名称));
}
}
}
公共类MainViewModel:INotifyPropertyChanged
{
私有列表_weightTypeList=新列表();
公共列表权重类型列表
{
获取{return\u weightTypeList;}
设置
{
_权重类型列表=值;
}
}
私人双_currentWeight;
公共双流权
{
获取{return\u currentWeight;}
设置
{
_当前权重=值;
NotifyPropertyChanged(“当前重量”);
}
}
私有权重类型\u选择的权重类型;
公共权重类型SelectedWeightType
{
获取{return\u selectedWeightType;}
设置
{
var previousType=\u selectedWeightType;
_selectedWeightType=值;
NotifyPropertyChanged(“SelectedWeightType”);
如果(previousType!=null)
CurrentWeight=(CurrentWeight/previousType.Factor)*\u选择的WeightType.Factor;
}
}
公共主视图模型()
{
添加(新的WeightType(){Id=1,UnitType=“Lbs”,Factor=1f});
添加(新的WeightType(){Id=2,UnitType=“Kg”,Factor=0.453592f});
重量
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;
}
}
}