2011-12-30 18 views
1

テキストが整数(たとえば123456789)をバインドする電子テキストボックスを作成したいが、1000個の区切り文字(たとえば123.456.789)が表示されますが、編集用のテキストボックスを選択すると、区切り、テキストボックスがフォーカスを失うまで、Excelと同じように。何かアドバイス?ありがとうございました!Excelのような振る舞いwpf textbox

答えて

2

利用のTextBoxはまた、あなたは簡単にStringFormatで書式を設定することができない場合の書式設定のためのコンバータを使用することができます

<Style TargetType="{x:Type TextBox}"> 
    <Setter Property="Text" Value="{Binding SomeValue, StringFormat=N2}" /> 
    <Style.Triggers> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="Text" Value="{Binding SomeValue}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

が選択されていない場合は、値をフォーマットトリガー

+0

ありがとう、ありがとうございます!ちょうど1つのこと、あなたは "、"から "千"の桁区切りをどのように変更できるのか知っていますか?直接xamlセッター式で? StackOverFlow Community、あなたは素晴らしいです:) –

+0

@GiacomoTagliabue 'StringFormat'の代わりに' Converter'を使わなければならないかもしれません。コンマをドットで '.Replace()'してください。 – Rachel

+0

ありがとうございます。私は 'Converter'を使用したくありませんでしたが、この場合は唯一の可能性があります。 –

0

一つの可能​​性:

IsFocusedにトリガーを作成するスタイルを追加します。 トリガーで、別のフォーマットを持つ新しいテンプレートを設定します。

<Grid> 
    <Grid.Resources> 
    <System:Double x:Key="boundDouble">1000</System:Double> 
    <System:Double x:Key="boundDouble2">2000</System:Double> 
    </Grid.Resources> 
    <TextBox Width="100" Height="30"> 
    <TextBox.Text> 
     <Binding Source="{StaticResource boundDouble}" Path="." StringFormat="{}{0:F3}" /> 
    </TextBox.Text> 
    <TextBox.Style> 
     <Style TargetType="TextBox"> 
     <Style.Triggers> 
      <Trigger Property="IsFocused" Value="true"> 
      <Setter Property="Template"> 
       <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <TextBox> 
        <TextBox.Text> 
        <Binding Source="{StaticResource boundDouble}" Path="." StringFormat="{}{0:F5}" /> 
        </TextBox.Text> 
        </TextBox> 
       </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      </Trigger> 
     </Style.Triggers> 
     </Style> 
    </TextBox.Style> 
    </TextBox> 
</Grid>