C# 文本框上的StringFormat阻止输入
我需要在文本框中显示和设置汇率。 My ViewModel包含一个名为C# 文本框上的StringFormat阻止输入,c#,wpf,mvvm,data-binding,decimal,C#,Wpf,Mvvm,Data Binding,Decimal,我需要在文本框中显示和设置汇率。 My ViewModel包含一个名为ExchangeRate的decimal属性。默认情况下,最多只能输入两位小数 汇率通常包含两位以上的小数位,因此我尝试使用StringFormat实现这一点: <TextBox HorizontalAlignment="Left" IsEnabled="{Binding ExchangeRateNeeded}" Text="{Binding Excha
ExchangeRate
的decimal
属性。默认情况下,最多只能输入两位小数
汇率通常包含两位以上的小数位,因此我尝试使用StringFormat实现这一点:
<TextBox HorizontalAlignment="Left"
IsEnabled="{Binding ExchangeRateNeeded}"
Text="{Binding ExchangeRate, UpdateSourceTrigger=PropertyChanged, Delay=250, Mode=TwoWay, StringFormat={}{0:0.00000}}"
Height="23" Margin="130,92,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="80" MaxLength="4"/>
您也可以显示您的ViewModel吗?
MaxLength=“4”
您确定要这样做吗?删除MaxLength=“4”哦,天哪,复制并粘贴。。。。这就解决了我的问题:(只是一个小提示,很少的例子真的很有帮助。如果你将文本框剥离到基本部分
,你会马上注意到除了绑定之外的其他问题。
public class SetupWindowViewModel : ViewModelBase
{
public ObservableCollection<SapVkorg> SapVkOrgs { get; }
public SapVkorg SelectedVkOrg { get; set; }
public ICommand OkCommand { get; }
public int Year { get; set; }
public bool ApertumNumbers { get; set; }
public ObservableCollection<Currency> Currencies { get; }
public Currency SelectedCurrency { get; set; }
public decimal ExchangeRate { get; set; }
public bool ExchangeRateNeeded { get { return SelectedCurrency != Currency.EUR; } }
public SetupWindowViewModel(Window window) : base(window)
{
OkCommand = new RelayCommand(Save, CanSave);
SapVkOrgs = mainController.GetSapVkorgs();
Currencies = mainController.GetCurrencies();
SelectedVkOrg = SettingsHolder.SapVkorg;
Year = SettingsHolder.Year;
ApertumNumbers = SettingsHolder.ApertumNumbers;
ExchangeRate = SettingsHolder.ExchangeRate;
SelectedCurrency = SettingsHolder.Currency;
}
public void Save()
{
SettingsHolder.SapVkorg = SelectedVkOrg;
SettingsHolder.Year = Year;
SettingsHolder.Currency = SelectedCurrency;
SettingsHolder.ExchangeRate = ExchangeRate;
SettingsHolder.ApertumNumbers = ApertumNumbers;
CloseWindow();
}
public bool CanSave()
{
return Year > 0 &&
SelectedVkOrg != null &&
((ExchangeRateNeeded && ExchangeRate != 1) || (!ExchangeRateNeeded));
}
}
[AddINotifyPropertyChangedInterface]
public abstract class ViewModelBase
{
protected readonly MainController mainController;
protected static readonly log4net.ILog log = LogHelper.GetLogger();
protected readonly Window window;
public ICommand CloseWindowCommand { get; }
protected ViewModelBase(Window window)
{
mainController = MainController.GetInstance();
this.window = window;
CloseWindowCommand = new RelayCommand(CloseWindow);
Initialize();
}
protected void CloseWindow()
{
window.Close();
}
protected bool? ShowDialog(Window windowToOpen)
{
windowToOpen.Owner = window;
return windowToOpen.ShowDialog();
}
private void Initialize()
{
window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
window.DataContext = this;
}
protected void DisplayAlertAndLogError(string message, Exception ex)
{
log.Error(message, ex);
MessageBox.Show(message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}