[英]Property change on one control to refresh converter on another?
我有一個頁面,用戶可以輸入他們的體重。 數據始終以磅為單位存儲,因為我在應用程序中的大部分計算都是基於磅。 但是,用戶可以從視覺上輸入公斤,磅和石頭。
因此,對於我的問題的示例,可以說我們有以下課程。
int UnitType
double Weight
現在,假設我有一個頁面,其中包含一個ComboBox,且SelectedIndex綁定到UnitType
。 ComboBox具有3個項目“ lbs”,“ kg”和“ st”。 在其下有一個文本框,您可以輸入一個值。 這將綁定到Weight
並將具有一個轉換器。
假設用戶選擇“ lbs”並輸入200,然后決定從lbs更改為kg。 我的問題是如何獲取ComboBox的section change事件以觸發文本框轉換器的刷新?
在我的腦海中,我猜想我將不得不做這樣的事情
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");
}
}
這項工作還是其他我可以做的,也許是在綁定本身中?
下面是實現此目標的一種可能方式:
一種。 在這里,我們使用一個因子變量來存儲權重的相對值。 我們將相對值存儲在構造函數中。
b。 1磅= 0.453592千克= 0.0714286f石頭。
C。 重量的轉換是在“選定的重量類型”中的set屬性設置的
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;
}
}
}
您可以擁有一個計算所得的屬性,該屬性知道如何轉換和顯示所選系統中的重量 。 您會在UnitType和Weight兩組上調用NotifyPropertyChanged(“ CalculatedWeight”)。
另外,我會用你的方法。 在Weight屬性的綁定中有一個轉換器,並將邏輯放在那里。
甚至更好的選擇是創建一個具有組合框和文本框的用戶控件,創建一個“ WeightProvider”類,並將userControl數據上下文綁定到該類,然后您有兩個選擇:
如果您要增加帶有權重的頁面,這是最有效的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.