WPF XamNumericEditor binded to Slider

本文关键字:Slider to binded XamNumericEditor WPF | 更新日期: 2023-09-27 18:27:29

我有一个XamNumeric编辑器和一个Slider来简化百分比输入。

问题是,一旦用户使用滑块更改值,编辑器中的旋转按钮就会被禁用。

我有以下xaml:

<InfragisticsWpf:XamNumericEditor x:Name="PercentEditor"
    Value="{Binding Percent, Mode=TwoWay}"                        
    ValueType="{x:Type System:Decimal}"
    Mask="nnn %"
    SpinButtonDisplayMode="Always">
    <InfragisticsWpf:XamNumericEditor.ValueConstraint>
        <InfragisticsWpf:ValueConstraint MinInclusive="0" MaxInclusive="100"/>
    </InfragisticsWpf:XamNumericEditor.ValueConstraint>
</InfragisticsWpf:XamNumericEditor>
<Slider 
    Value="{Binding ElementName=PercentEditor, Path=Value, Mode=TwoWay, Converter={StaticResource DoubleToDecimalConverter}}" 
    Minimum="0" 
    Maximum="100"                        
    IsSnapToTickEnabled="True" 
    VerticalContentAlignment="Center"
    VerticalAlignment="Center"
    TickFrequency="5">                      
</Slider>

有什么想法吗?

WPF XamNumericEditor binded to Slider

经过更多的调试,我找到了一个解决方案。XamNumericEditor的问题是,在其他控件的绑定上,它将carret位置设置在可见值(在我的情况下为%)的末尾,这就是旋转按钮不起作用的原因。为了纠正这个问题,我添加了以下处理程序:

void NumericEditorOnGotFocus(object sender, RoutedEventArgs e)
{
	var numericEditor = sender as XamNumericEditor;
	if (numericEditor != null)
	{
		var text = numericEditor.Text;
		var numberMatch = Regex.Match(text, @"'d");
		if (numberMatch.Success)
		{
                    numericEditor.SelectionStart = numberMatch.Index;
		}
	}
}

我知道这不是最好的主意,因为我已经在后面的代码中添加了处理程序。我更希望直接在Xaml文件中有一个解决方案,但这解决了问题。