简体   繁体   中英

Best way to show a different Decimal Precision when TextBox is focused or not?

I have control, that is used by others. It has a TexBox that is binding to a decimal value. And since the Precision can vary, I am using a DependencyProperty and MultiBinding to specify it. When the TextBox is not focused the number should be displayed with the specified Precision, but when it is focused the full number should be displayed.

Example:

  • Precision: 2
  • User Input: 29.333
  • TextBox not focused should show: 29.33
  • TextBox focused should show: 29.333

I have accomplished this by using a IMultibindingConverter, and the IsFocused property. But I don't know if this is the best approach.

My TexBox is defined like this

  <UserControl.Resources>
        <conv:ValuePrecisionConverter x:Key="ValuePrecisionConverter" />        
    </UserControl.Resources>
       <TextBox x:Name="myTextBox">
                 <TextBox.Text>
                 <MultiBinding Converter="{StaticResource ValuePrecisionConverter}"  Mode="TwoWay"
     NotifyOnValidationError="true">
                     <Binding ElementName="parent" UpdateSourceTrigger="PropertyChanged" Path="Value" Mode="TwoWay" />
                     <Binding ElementName="parent" Path="Precision"/>
                     <Binding ElementName="parent" Path="AdditionalFormatting"/>
                     <Binding ElementName="myTextBox" Path="IsFocused" Mode="OneWay"/>
                     </MultiBinding>
                 </TextBox.Text>           
       </TextBox>

My ValuePrecisionConver is defined like this:

 public class ValuePrecisionConverter : IMultiValueConverter
    {
         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            double? value = null;
            converter = null;

            value = System.Convert.ToDouble(values[0]);
            precision = System.Convert.ToInt32(values[1]);
            //Other code with 3rd parameter

            if (values.Count() > 3)
            {
                bool isFocused = System.Convert.ToBoolean(values[3]);

                if (isFocused)
                    return value.ToString();
            }

           /*Here I do the formating with the given precision*/
           return formattedValue;
        }


}

Is this the best way to accomplish what I need? Is it ok to use the IsFocused property like this?

Try with thisss.

<TextBox x:Name="myTextBox">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="False">
                     <Setter Property="Text" Value="{Binding Value, StringFormat=n2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                     <Setter Property="Text" Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                 </Trigger>
             </Style.Triggers>
        </Style>
   </TextBox.Style>
</TextBox>

我希望您去使用带有PreviewGotKeyboardFocus和PreviewLostKeyboardFocus的事件触发器...用转换器设置值。在这里不需要MuiltBinding ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM