2

私は WPF アプリケーションを作成しており、スライダーをテキスト ボックスにバインドしており、その逆も同様です。

私の問題は、テキスト ボックスの値を変更するときに、スライダーの値が更新される前にテキスト ボックスの外をクリックする必要があることです。

ユーザーがテキストボックスにテキストを入力したとき、またはユーザーがテキストボックス内でEnterキーを押したときに、スライダーの値を変更したいと思います。

私が持っているコードは次のとおりです。

XAML:

<Slider Name="sldrConstructionCoreSupplierResin" Minimum="0" Maximum="10" Grid.Column="1" Grid.Row="1" IsSnapToTickEnabled="True"/>
    <TextBox Name="txtConstructionCoreSupplierResin" Text="{Binding ElementName=sldrConstructionCoreSupplierResin, Path=Value, Converter={StaticResource RoundingConverter}}" Grid.Column="2" Grid.Row="1"/>

コードビハインド:

public class RoundingConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                double dblValue = (double)value;
                return (int)dblValue;
            }
            return 0;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                int ret = int.Parse(value.ToString());
                return ret;
            }
            return 0;
        }
    } 
4

2 に答える 2

4

デフォルトでは、コントロールがフォーカスを失うとバインディングが更新されます。そのため、TextBox の外をクリックしたときにのみ変更が表示されます。これは、次のように Binding のUpdateSourceTriggerプロパティを使用して変更できます。

<TextBox Text="{Binding Value,ElementName=mySlider,UpdateSourceTrigger=PropertyChanged}" />

これで、TextBox は、フォーカスを失ったときではなく、Text プロパティが変更されるたびにソースを更新します。

于 2012-09-14T09:45:12.527 に答える
1

UpdateSourceTriggerのプロパティをに設定しBindingますPropertyChanged

<TextBox Text="{Binding ElementName=sldrConstructionCoreSupplierResin, Path=Value, UpdateSourceTrigger = "PropertyChanged" Converter={StaticResource RoundingConverter}}"/>

詳細はこちら

于 2012-09-14T09:47:46.060 に答える