2012-04-09 115 views
2

我希望用戶能夠輸入310312並且自動將日期選擇器的文本屬性更新爲31/03/12;我已經將日期選擇器綁定到視圖模型'日期'屬性如下。Wpf DatePicker文本在綁定源更新後未更新

使用WPF4.0,綁定現在自動執行一組get(不需要INotifyPropertyChanged);這發生在下面的代碼中,但雖然'get'日期字段值是正確的'31/03/12',但datepicker文本屬性未更新,並且保持在310312(NB UpdateSourceTrigger = PropertyChanged)。

文本框屬性確實變化(例如在設置代碼,未顯示,轉換爲大寫)

我真的很感激一些指針,因爲這是爲什麼。

 <Grid> 
     <DatePicker Text="{Binding Path=Date,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
      Height="25" HorizontalAlignment="Left" Name="datePicker1"/> 
     <TextBox Text="{Binding Path=State,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Name="textBox1" /> 
    </Grid> 

     private string date; 
     public string Date 
     { 
      get 
      { 
       return date; 
      } 
      set 
      { 
       if (value != null) 
       { 
        Regex abbreviatedDateFormat = new Regex(@"\A\d{6}\Z"); 
        if (abbreviatedDateFormat.IsMatch(value)) 
        { 
         value = value.Insert(2, "/"); 
         value = value.Insert(5, "/"); 
        } 
       } 

       date = value;      
      } 
     } 
+0

我想達到同樣的效果。你有沒有找到解決方案? – mosquito87 2015-02-23 17:40:14

回答

0

這不是一個明確的答案,但我想發佈代碼並將其格式化,所以我把它放在這裏。

這讓我很感興趣,所以我一直在玩一個簡單的測試項目。這裏的是我的代碼:

MainWindow.xaml

<Window x:Class="datepickertest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525" x:Name="Me"> 
    <StackPanel> 
     <DatePicker Name="datePicker" Text="{Binding ElementName=Me, Path=Date, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 

     <TextBlock Text="{Binding ElementName=datePicker, Path=Text}"/> 
     <TextBlock Text="{Binding ElementName=datePicker, Path=SelectedDate}"/> 
     <TextBlock Text="{Binding ElementName=Me, Path=Date}"/> 
    </StackPanel> 
</Window> 

MainWindow.xaml.cs

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
    Date = "030912"; 
    InitializeComponent();   
    } 

    private string date; 
    public string Date 
    { 
    get { return date; } 
    set 
     { 
      if (value != null) 
      { 
       Regex abbreviatedDateFormat = new Regex(@"\A\d{6}\Z"); 
       if (abbreviatedDateFormat.IsMatch(value)) 
       { 
        value = value.Insert(2, "/"); 
        value = value.Insert(5, "/"); 
       } 
      } 

      date = value;      
     } 
    } 

}

什麼我注意到對於輸入日期時間控制不理解日期格式,如果它們沒有用'/'字符分隔。我將Text屬性綁定到像您一樣的TextBlock,我可以看到它按字符更新TextBlock字符,並且在匹配正則表達式時斜線顯示,但實際輸入框值不更新(不管UpdateSourceTrigger),直到提供有效日期並按下回車鍵。在我看來,這可能是控制的期望行爲。

我知道控件的基礎是當前線程的文化信息,但在此鏈接http://social.msdn.microsoft.com/Forums/en/wpf/thread/f065bcda-a5df-4fd1-bd29-3d0186245c8c之後,我仍然無法找到適合您的解決方案。

+0

謝謝你看這個 - 我看不到datepicker在綁定讀取viewmodel值後不讓綁定更新它的文本的原因。我會想象綁定嘗試在datepicker上設置更改的值,但datepicker拒絕播放球。看起來很奇怪,如果它是由設計。 – sturdytree 2012-04-09 20:19:20

+0

我同意。我想如果我可以設置Text屬性在構造過程中綁定的值,並且日期選擇器輸入將其選中,則可以認爲它在使用set/get組合時的行爲相似。 – Josh 2012-04-10 12:43:01