我有一個來自 WinForms 的代碼,當按下文本框上的一個鍵時,檢查輸入了哪個字符,如果它是一個點,那么它會變成一個逗號,如果它是別的東西,那么它會檢查它是否是一個數字與否
private void textbox_KeyPress(object sender, KeyPressEventArgs e)
{
char ch = e.KeyChar;
if (ch == '.')
{
e.KeyChar = ',';
}
if ((!Char.IsDigit(ch)) && (ch != ',') && (ch != '.') && (ch != 8))
{
e.Handled = true;
}
}
如何在 WPF 上實作這樣的代碼
現在代碼是這樣的:
<TextBox
PreviewTextInput="NumberValidationTextBox"
/>
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9,.] ");
e.Handled = regex.IsMatch(e.Text);
}
在 TextBox 中,您現在可以輸入數字、點和逗號,它仍然只是將點更改為逗號,作為使用 textchanged 事件的一個選項,但是因為我需要這個事件來處理點已經存在的另一個代碼改為逗號,則不能使用此事件。
uj5u.com熱心網友回復:
結果,此代碼有效:
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9,.] ");
e.Handled = regex.IsMatch(e.Text);
Regex regex1 = new Regex(",");
if (regex1.IsMatch(e.Text))
{
e.Handled = true;
tb1.Text = ".";
tb1.CaretIndex = tb1.Text.Length;
}
}
uj5u.com熱心網友回復:
也許您可以將發件人轉換為 aTextBox然后以這種方式設定文本?喜歡:
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9,.] ");
e.Handled = regex.IsMatch(e.Text);
if (sender is TextBox textbox)
{
var currentText = textbox.Text;
textbox.Text = currentText.Replace('.', ',');
}
}
很難弄清楚你的意圖是什么。
您可能只想訂閱TextChanged文本條目上的事件并在資訊進入時對其進行修改。在該方法中,您應該能夠根據自己的喜好更新文本,并確認文本有效。
請參閱此處的 Microsoft 檔案。
以下是您可以如何執行此操作的示例:
<TextBox TextChanged="Entry_TextChanged"/>
private void Entry_TextChanged(object sender, TextChangedEventArgs e)
{
//textBox.TextChanged -= Entry_TextChanged;
//textBox.Text = textBox.Text.Replace(".",".");
//textBox.TextChanged = Entry_TextChanged;
}
進一步的研究表明,微軟有關于系結驗證的檔案,我相信它會適合你的情況。你可以在這里閱讀更多關于它的資訊。
下面是一個如何在 WPF 中實作驗證規則的示例,其中 XAML 顯示了ValidationRule物件的實體化,而 C# 顯示了它ValidationRule是如何實作的:
<TextBox Name="textBox1" Width="50" FontSize="15"
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Style="{StaticResource textBoxInError}"
Grid.Row="1" Grid.Column="1" Margin="2">
<TextBox.Text>
<Binding Path="Age" Source="{StaticResource ods}"
UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<c:AgeRangeRule Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
public class AgeRangeRule : ValidationRule
{
public int Min { get; set; }
public int Max { get; set; }
public AgeRangeRule()
{
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
int age = 0;
try
{
if (((string)value).Length > 0)
age = Int32.Parse((String)value);
}
catch (Exception e)
{
return new ValidationResult(false, $"Illegal characters or {e.Message}");
}
if ((age < Min) || (age > Max))
{
return new ValidationResult(false,
$"Please enter an age in the range: {Min}-{Max}.");
}
return ValidationResult.ValidResult;
}
}
如果這不起作用,那么這個答案可能會有所幫助:如何讓 TextBox 只接受 WPF 中的數字輸入?
否則,這也可以通過實作某種形式的DataTrigger.
A DataTrigger“表示當系結資料滿足指定條件時應用屬性值或執行操作的觸發器。”
請參閱此處的 Microsoft 檔案。
ListBoxItem從他們的頁面中提取的一個示例顯示了如何根據物件的 text 屬性有條件地更改前景色Place:
<Window.Resources>
<c:Places x:Key="PlacesData"/>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State}" Value="WA">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name}" Value="Portland" />
<Condition Binding="{Binding Path=State}" Value="OR" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Cyan" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
<DataTemplate DataType="{x:Type c:Place}">
<Canvas Width="160" Height="20">
<TextBlock FontSize="12"
Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
<TextBlock FontSize="12" Width="30"
Canvas.Left="130" Text="{Binding Path=State}"/>
</Canvas>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock FontSize="18" Margin="5" FontWeight="Bold"
HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
<ListBox Width="180" HorizontalAlignment="Center" Background="Honeydew"
ItemsSource="{Binding Source={StaticResource PlacesData}}"/>
</StackPanel>
或者(或者可能另外),您可能需要在系結中使用某種轉換器。可以有一個字串轉換器,將一個字符的所有實體替換為另一個字符,并將轉換后的字串回傳到系結。在此處查看 Microsoft 關于轉換器的一些檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489388.html
