2

キーストロークごとにテキストバインディングを更新する方法はありますか?

私の WPF カスタム TextBox は KeyUp イベントを使用します

private void MyTextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    BindingExpression BE = GetBindingExpression(TextBox.TextProperty);
    if (BE != null)
        BE.UpdateSource();
}

しかし、 Windows 8/RT/StoreBindingExpressionには存在しません。

4

5 に答える 5

5

質問が少し古いことは知っていますが、今日この問題に遭遇し、非常に簡単な解決策を見つけました。多分それは誰かを助ける:

<TextBox Text="{Binding BindingSource, UpdateSourceTrigger=PropertyChanged}"/>

UpdateSourceTrigger-Property を "PropertyChanged" に設定すると、テキストが変更されるたびに (フォーカスが失われていない場合でも)、UpdateSource() メソッドが呼び出されます。

于 2014-03-24T19:30:50.170 に答える
1

さて、テキストは正しくバインドされていますか? バインド先のオブジェクトに INotifyPropertyChanged インターフェイスが継承されていること、および NotifyProperty() イベントであるパブリック文字列プロパティを呼び出していることを確認してください。

したがって、すべてが適切にバインドされている場合は、TextBox Text を設定するだけで、XAML が値を自動的に更新します。

データバインディングに関するチュートリアルは次のとおりです: http://msdn.microsoft.com/en-us/library/ms752347.aspx

于 2012-08-31T15:20:05.677 に答える
1

またはTextChangedではなく使用する必要がありますKeyUpKeyDown

C# を使用して WinRT で TextBox のソースをすぐに更新する方法

于 2013-09-16T17:52:28.703 に答える
1

添付プロパティを使用して問題を解決する方法があります。TextBox.Text を機能させる方法の記事を確認してください。

于 2013-10-02T08:12:23.767 に答える
0

このタスクを達成する方法を提案できます。これは、Silverlightの他のバージョン (WPF およびprimisの Windows Phone ) でより簡単になります...

BindingExpressionご覧のとおり、Windows RT/Store アプリケーションにはありません!...

MVVMパターンと互換性のあるこのコードを実装します...

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace YourProject
{
    public static class TextBoxEx
    {
        public static string GetRealTimeText(TextBox obj)
        {
            return (string)obj.GetValue(RealTimeTextProperty);
        }

        public static void SetRealTimeText(TextBox obj, string value)
        {
            obj.SetValue(RealTimeTextProperty, value);
        }

        public static readonly DependencyProperty RealTimeTextProperty = DependencyProperty.RegisterAttached("RealTimeText", typeof(string), typeof(TextBoxEx), null);

        public static bool GetIsAutoUpdate(TextBox obj)
        {
            return (bool)obj.GetValue(IsAutoUpdateProperty);
        }

        public static void SetIsAutoUpdate(TextBox obj, bool value)
        {
            obj.SetValue(IsAutoUpdateProperty, value);
        }

        public static readonly DependencyProperty IsAutoUpdateProperty =
            DependencyProperty.RegisterAttached("IsAutoUpdate", typeof(bool), typeof(TextBoxEx), new PropertyMetadata(false, OnIsAutoUpdateChanged));

        private static void OnIsAutoUpdateChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var textbox = (TextBox)sender;

            if ((bool)e.NewValue)
                textbox.TextChanged += textbox_TextChanged;
            else
                textbox.TextChanged -= textbox_TextChanged;
        }

        private static void textbox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textbox = (TextBox)sender;

            textbox.SetValue(TextBoxEx.RealTimeTextProperty, textbox.Text);
        }
    }
}

...そして、以下に示すように、小さなトリック (二重バインディング) を使用して XAML で使用するだけです...

1) 新しい「ユーティリティ」クラスを宣言します。

<common:LayoutAwarePage x:Name="pageRoot"
    x:Class="YourProject.YourPage"
    DataContext="{Binding Path=DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="YourProject"
    ...
    xmlns:extra="using:YourProject.YourNameSpace"
    mc:Ignorable="d">

2) コントロールへの実装:

<TextBox extra:TextBoxEx.IsAutoUpdate="True"
         extra:TextBoxEx.RealTimeText="{Binding Path=YourTextProperty, Mode=TwoWay}">
    <TextBox.Text>
        <Binding Path="YourTextProperty"
                 Mode="OneWay" />
    </TextBox.Text>
</TextBox>

これは私にとってはうまくいきました。

于 2013-09-16T09:09:55.003 に答える