2012-03-16 33 views
0

我使用的擴展WPF工具包,並想更新值當用戶鍵入的東西在(的PropertyChanged絲帶引發LostFocus +的NumericUpDown發出

但是,在使用IntegerUpDown(或任何與此有關NumericUpDown控件時)該值只會在輸入鍵按下時更新,或者如果您按下控制器上的向下按鈕,或者控制器失去焦點。這一直是「設計」由開發者在這裏指出:

http://wpftoolkit.codeplex.com/workitem/16544

「這是設計的源纔會被當值增加/減少更新上的Enter鍵按,或。失去了重點「。

設置UpdateSourceTrigger爲任意值(即的PropertyChanged)沒有任何影響,這也已在此SO線程記載:

DecimalUpDown (Extended WPF toolkit) - Source only gets updated on lost focus

但是我遇到了嚴重的問題,這種行爲時將它與一條絲帶結合使用,當打開LostFocus時,它實際上不會真正觸發LostFocus。因此,根據綁定到NumericUpDown的值將任何命令放入功能區將無法按預期工作。

我一直在想自己觸發LostFocus事件,只要用戶打開功能區,但似乎是醜陋的,可能不會一直工作。

我在尋找一個優雅的解決方案來解決這個問題,我真的不想下載源代碼並修改它。我想要修改NumericUpDown控件來實際更新PropertyChanged上的值(這可以使用額外的XAML/Behaviors/Triggers來完成嗎?),或者找到一種優雅的方式來觸發LostFocus事件,只要我想實際上不會毀掉用戶體驗。

有沒有人找到解決方案?

+0

的問題是,你不」不知道用戶何時停止在文本框中輸入內容。很難用優雅的方式解決。 – 2012-03-16 12:15:09

+0

@HansPassant我不會嘗試 - 我想更新屬性,每當值更改,就像它與文本框一起工作 – UrbanEsc 2012-03-16 12:20:45

回答

0

我已經通過重寫控件的默認模板解決了這個問題

<Style x:Key="EnhancedNumericUpDown" TargetType="Control"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Control"> 
        <ext:ButtonSpinner x:Name="Spinner" 
            IsTabStop="False" 
            Background="{TemplateBinding Background}" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            AllowSpin="{Binding AllowSpin, RelativeSource={RelativeSource TemplatedParent}}" 
            ShowButtonSpinner="{Binding ShowButtonSpinner, RelativeSource={RelativeSource TemplatedParent}}"> 
         <ext:WatermarkTextBox x:Name="TextBox" 
              BorderThickness="0" 
              Background="Transparent" 
              ContextMenu="{TemplateBinding ContextMenu}" 
              FontFamily="{TemplateBinding FontFamily}" 
              FontSize="{TemplateBinding FontSize}" 
              FontStretch="{TemplateBinding FontStretch}" 
              FontStyle="{TemplateBinding FontStyle}" 
              FontWeight="{TemplateBinding FontWeight}" 
              Foreground="{TemplateBinding Foreground}" 
              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
              IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" 
              MinWidth="20" 
              AcceptsReturn="False" 
              Padding="0" 
              SelectAllOnGotFocus="{Binding SelectAllOnGotFocus, RelativeSource={RelativeSource TemplatedParent}}" 
              TextAlignment="{Binding TextAlignment, RelativeSource={RelativeSource TemplatedParent}}" 
              TextWrapping="NoWrap" 
              TabIndex="{TemplateBinding TabIndex}" 
              Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}" 
              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
              Watermark="{Binding Watermark, RelativeSource={RelativeSource TemplatedParent}}" 
              WatermarkTemplate="{Binding WatermarkTemplate, RelativeSource={RelativeSource TemplatedParent}}" /> 
        </ext:ButtonSpinner> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

我改變了模板綁定Text屬性,包括觸發

Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"