説明したシナリオをテストしましたが、正常に機能します。
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メソッドが呼び出されます。
バインディングが正常に機能しますか?