2016-04-20 80 views
-1

問題: DecimalTextBox已經值設置爲「」 txtSickTime.Text = newTest.PutThisDecimalInTheBox.ToString();TextBox.Text值不更新時被設置

可變txtSickTime.Text = newTest.PutThisDecimalInTheBox.ToString();有它是在Visual Studio如320.5所示值的代碼行後,所以不宜此當轉換爲一個字符串可以放入DecimalTextBox沒有錯誤?

前端

<Window 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:MegA="clr-namespace:MegA;assembly=MegA" x:Class="WpfApplication14.MainWindow" 
     Title="MainWindow" Height="350" Width="525" 
     Loaded="Window_Loaded"> 
    <Grid> 
     <MegA:DecimalTextBox DollarPrecision="12" Height="22.864" Name="txtSickTime" Width="60.00" MaxLength="4" TabIndex="230" DecimalPrecision="2"/> 
    </Grid> 
</Window> 

後端

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Windows.Forms; 

namespace WpfApplication14 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      Test newTest = new Test(); 
      txtSickTime.Text = newTest.PutThisDecimalInTheBox.ToString(); 
     } 
    } 
    public class Test 
    { 
     public Decimal PutThisDecimalInTheBox; 
     public Test() 
     { 
      PutThisDecimalInTheBox = 320.500m; 
     } 
    } 
    public class DecimalTextBox : System.Windows.Controls.TextBox 
    {//this is a modified textbox that takes in decimal values also modified to highlight all text on focus 
     bool alreadyFocused; 
     private string PreviousText; 
     string _filterString; 
     int _dollarPrecision; 
     int _decimalPrecision; 
     public string FilterString 
     { 
      get { return _filterString; } 
      set { _filterString = value; } 
     } 

     public int DollarPrecision 
     { 
      get { return _dollarPrecision; } 
      set { _dollarPrecision = value; } 
     } 

     public int DecimalPrecision 
     { 
      get { return _decimalPrecision; } 
      set { _decimalPrecision = value; } 
     } 
     public decimal TextDecimal 
     { 
      get { return Convert.ToDecimal(this.Text); } 
     } 


     public DecimalTextBox() 
     { 
      TextAlignment = System.Windows.TextAlignment.Right; 
      FontFamily = new System.Windows.Media.FontFamily("Courier New"); 
      FilterString = "-1234567890."; 
      this.TextChanged += DecimalTextBox_TextChanged;//add the events 
     } 
     //event function that limits the input in the text box 
     private void DecimalTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) 
     { 
      this.TextChanged -= DecimalTextBox_TextChanged;//remove the event so it doesn't get called again 
      //get the cursor location to fix it later 
      int location = this.SelectionStart; 
      //don't allow anything except the filter string 
      if ((!string.IsNullOrEmpty(FilterString))) 
      { 
       if (this.Text.Trim().Length > 0) 
       { 
        for (int i = 0; i <= this.Text.Length - 1; i++) 
        { 
         if (!(this.FilterString.Contains(this.Text.Substring(i, 1)))) 
         { 
          this.Text = this.Text.Remove(i, 1); 
         } 
        } 
       } 
       string tempText = Text.Replace("-", ""); 
       string[] splitAtDecimal = tempText.Split('.'); 
       if (splitAtDecimal.Length > 2) 
       { 
        Text = PreviousText; 
       } 
       else if (splitAtDecimal.Length == 2) 
       { 
        if (splitAtDecimal[0].Length > DollarPrecision)//if violating dollar or decimal precision return to previoustext 
        { 
         Text = PreviousText; 
        } 
        if (splitAtDecimal[1].Length > DecimalPrecision) 
        { 
         Text = PreviousText; 
        } 
       } 
       //set the PreviousText=Text for comparison next time textchanged is called 
       PreviousText = Text; 
       //this.SelectionStart = this.Text.Length; 
       this.TextChanged += DecimalTextBox_TextChanged;//add the event back 
      } 
     } 
    } 
} 
+1

沒有一個好的[mcve]可以可靠地重現問題,所以不可能回答這個問題。我會這樣說:如果你學習了WPF的預期習慣用法,那麼從長遠來看,你的代碼會更容易編寫。你應該使用數據綁定而不是直接設置控制值,並且你不應該爲了使這個工作而繼承'TextBox'。使用轉換器進行綁定和驗證規則以驗證輸入將會帶來更好的體驗。似乎花一些時間研究這些主題將會對您的時間產生有益的影響。 –

+0

我覺得很難重現這個問題,因爲我對此感到不知所措。 – Scriven

+0

我與@PeterDuniho達成協議。如果您使用的是WPF,那麼您的代碼隱藏應該僅限於ViewModel填充以及WPF本身無法處理的其他業務規則。但是,顯示值應該使用數據綁定來完成,並且值可以與轉換器一起使用來執行任何操作(數據類型到另一個,更改控制屬性等)。 –

回答

1

問題發生的原因是因爲,儘管視覺工作室顯示爲320.5,當它被轉換成一個字符串十進制的值它保留那些2個尾隨零。

因此你的字符串將是320.500。

在這種情況下,問題的答案是放棄尾部零或者向DecimalTextBox的DecimalPrecision添加+1,否則它將恢復爲DecimalTextBox的原始值「」。

就我個人而言,我認爲它應該顯示尾隨零,如果它將在事後保留它們。

相關問題