Skip to content

Instantly share code, notes, and snippets.

@dvbobrov
Created April 9, 2015 19:12
Show Gist options
  • Save dvbobrov/6f13176f9eeffebae355 to your computer and use it in GitHub Desktop.
Save dvbobrov/6f13176f9eeffebae355 to your computer and use it in GitHub Desktop.
WPF backwards compatibility issue
namespace WpfApplication1
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using WpfApplication1.Annotations;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class ViewModel : INotifyPropertyChanged
{
private string _value;
public string Value
{
get
{
return _value;
}
set
{
if (value == _value)
{
return;
}
_value = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Converter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.Print($"Convert value: {value}, parameter: {parameter}");
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
Debug.Print($"ConvertBack value: {value}, parameter: {parameter}");
return value;
}
}
}
<Window x:Class="WpfApplication1.MainWindow"
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="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:ViewModel}"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<local:Converter x:Key="Conv" />
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<TextBox Text="{Binding Value, Converter={StaticResource Conv},
ConverterParameter='param', UpdateSourceTrigger=LostFocus}" />
<TextBox />
</StackPanel>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment