このタスクを達成する方法を提案できます。これは、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>
これは私にとってはうまくいきました。