当 TextBox 的焦点在 WPF 中丢失时更改 StringFormat
Change the StringFormat when the focus from TextBox is lost in WPF
有一个文本框。我正在为它输入一个值。并保存它。取回值时,它显示为十进制。但我希望它在文本框失去焦点后显示为小数。
<DataGridTemplateColumn Header="Add-Item" Width="2.25*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding AddItem ,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,StringFormat=N2}" Margin="6,5,4,5" helpers:TextBoxExtension.ValidationType="DecimalSpecialCharacter">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyUp" >
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
因为我需要允许空白文本框,所以我将其作为字符串类型。
private string _addItem = string.Empty;
public string AddItem
{
get => _addItem;
set
{
if (_addItem != value)
{
_addItem = value;
RaisePropertyChangedEvent("AddItem");
}
}
}
让文本框在失去焦点时触发添加 .00:
在您的虚拟机中:
// Constructor
public YourViewModel()
{
LostFocusCommand = new DelegateCommand(this.LostFocus);
}
public ICommand LostFocusCommand { get; }
private void LostFocus()
{
if(decimal.TryParse(addItem, out var dec))
{
var rounded = Math.Round(dec, 2); // round to 2 decimals
this.AddItem = rounded.ToString("F2"); // or "N2"
}
}
在您的 xaml 中,在该特定文本框中添加另一个触发器
<i:EventTrigger EventName="LostFocus" >
<i:InvokeCommandAction Command="{Binding Path=DataContext.LostFocusCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" />
</i:EventTrigger>
有一个文本框。我正在为它输入一个值。并保存它。取回值时,它显示为十进制。但我希望它在文本框失去焦点后显示为小数。
<DataGridTemplateColumn Header="Add-Item" Width="2.25*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding AddItem ,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,StringFormat=N2}" Margin="6,5,4,5" helpers:TextBoxExtension.ValidationType="DecimalSpecialCharacter">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyUp" >
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
因为我需要允许空白文本框,所以我将其作为字符串类型。
private string _addItem = string.Empty;
public string AddItem
{
get => _addItem;
set
{
if (_addItem != value)
{
_addItem = value;
RaisePropertyChangedEvent("AddItem");
}
}
}
让文本框在失去焦点时触发添加 .00:
在您的虚拟机中:
// Constructor
public YourViewModel()
{
LostFocusCommand = new DelegateCommand(this.LostFocus);
}
public ICommand LostFocusCommand { get; }
private void LostFocus()
{
if(decimal.TryParse(addItem, out var dec))
{
var rounded = Math.Round(dec, 2); // round to 2 decimals
this.AddItem = rounded.ToString("F2"); // or "N2"
}
}
在您的 xaml 中,在该特定文本框中添加另一个触发器
<i:EventTrigger EventName="LostFocus" >
<i:InvokeCommandAction Command="{Binding Path=DataContext.LostFocusCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}" />
</i:EventTrigger>