SilverLight

 BindingのStringFormatを使用して、生データを加工し、必要なフォーマットでコントロールにバインディングすることを紹介します。  簡単の利用例

//XAML
<TextBox Text="{Binding PaymentAmount, StringFormat=C, ConverterCulture=ja-JP}" />
//バインディングデータは「12」の場合、表示する文字列は「\12」となります。

//ソースコード
this.TextBox1.SetBinding(
     TextBox.TextProperty,
     new System.Windows.Data.Binding("PaymentAmount")
          { 
              StringFormat="C",
              ConverterCulture = new System.Globalization.CultureInfo("ja-JP")
          }
);

StringFormatに利用できる整形ルールは下記のMSDNに参考ください。

MSDN情報:
http://msdn.microsoft.com/ja-jp/library/26etazsy.aspx

整形するデータのカスタマイズ

IValueConverterインタフェースの実装によって、実現します。

下記のサンプルは、bool型のデータをバインディングする際に、データの値によって、対象のコントロールを表示・非表示にするサンプル

using System.Windows.Data;

public class BoolToVisibilityConverter : IValueConverter
{ 
	//バインディングデータから対象の表示フォーマットへの変換
	public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
	{
		if (value is bool)
		{
			if (targetType == typeof(Visibility))
			{
				return ((bool)value) ? Visibility.Visible : Visibility.Collapsed;
			}
		}
		return Visibility.Collapsed;
	}

	//対象の表示フォーマットからバインディングデータへの変換
	public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture
	{
		if (value.GetType() == typeof(Visibility))
		{
			if (targetType == typeof(bool))
			{
				return (((Visibility)value) == Visibility.Visible) ? true : false;
			}
		}
		return false;
	}
	
}

利用する側のXAMLに下記のように記述します。

<UserControl.Resources>
   <converters:BoolToVisibilityConverter x:Key="boolToVisibilityConverter"/>
</UserControl.Resources>

<TextBox Visibility="{Binding IsVisible, Converter={StaticResource boolToVisibilityConverter}}" />


あなたのお住まいの地域で最安のブロードバンド選び

コメント:



(画像の文字列を入力して下さい)

トップ   編集 凍結解除 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:36:07 (1628d)

PCpዾyǗlgĂ܂}WŔ܂z 萔O~ył񂫁z Yahoo yV NTT-X Store

z[y[W ̃NWbgJ[h COiq ӂ邳Ɣ[ COsیI COze