2

単純なWinRT(Win8、.NET / C#/ XAML)プロジェクトがあります。XAML TextBoxコントロールの1つに、ビューモデルからのデータバインド値をフォーマットするカスタムStringValueConverterがアタッチされています。

これはうまく機能しますが、1つ欠けていることがあります。ユーザーがTextBoxの値(たとえば、通貨の値)を変更し、TextBoxを離れると、コンバーターが自動的に適用される必要があります。これまでのところ、ビューモデルのデータバインド値は更新されていますが、コンバータはビューによって再度適用されません。

このソリューションまたは既知のカスタムソリューションの組み込みソリューションはありますか?

4

1 に答える 1

0

説明したシナリオをテストしましたが、正常に機能します。

XAML:

<Window x:Class="ValueConverterTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:valueConverterTest="clr-namespace:ValueConverterTest"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <valueConverterTest:CustomConverter x:Key="CustomConverter" />
</Window.Resources>
  <StackPanel>
   <TextBox Text="{Binding CustomText, Converter={StaticResource CustomConverter}}"></TextBox>
    <TextBox></TextBox>
</StackPanel>
</Window>

背後にあるコード:

namespace ValueConverterTest
{
   using System.ComponentModel;
   using System.Windows;

   public partial class MainWindow : Window, INotifyPropertyChanged 
   {

      public string CustomText
      {
        get { return customText; }
        set
        {
           customText = value;
           OnPropertyChanged("CustomText");
        }
      }

      public MainWindow()
      {
        InitializeComponent();
        DataContext = this;
      }

      private string customText;

      public event PropertyChangedEventHandler PropertyChanged;

      protected virtual void OnPropertyChanged(string propertyName)
      {
         PropertyChangedEventHandler handler = PropertyChanged;
         if (handler != null)
         {
            handler(this, new PropertyChangedEventArgs(propertyName));
         }
      }
   }
}

ValueConverter:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace ValueConverterTest
{
   public class CustomConverter : IValueConverter
   {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
         return value;
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
         return value;
      }
   }
}

このサンプルは正常に機能します。テキストボックスを離れると、最初にconvert backメソッドが呼び出され、次にconvertメソッドが呼び出されます。

バインディングが正常に機能しますか?

于 2013-03-06T16:04:27.777 に答える