• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Bug about TwoWay DataBinding & Coerce function



In sample, we have two control (our custom control and one slider)
which do data binding in two ways.

And we set a coerce function callback for one dependency property in
our custom control.
We change our property by clicking button and found strange behavior at
boundary value ( 5 in this case)

We found that our value is contrant by coerce function and not trigger
OnPropertyChange but the value of slider is changed to 6 which is not a
legal value.

1. Click Button Six times
Actual Result------------------------------------
value of text is 5 and the value of the slider is 6
Expected Result------------------------------------
value of text and the value of slides is the same, 5

The code below
public class CustomControl1 : System.Windows.Controls.Control
static CustomControl1()
//This OverrideMetadata call tells the system that this
element wants to provide a style that is different than its base class.
//This style is defined in themes\generic.xaml

DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new

public int Value
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }

// Using a DependencyProperty as the backing store for Value.
This enables animation, styling, binding, etc...
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(int),
typeof(CustomControl1), new UIPropertyMetadata(0,
OnColorChannelChanged, CoerceColorChannel), new

protected override void
OnPropertyChanged(DependencyPropertyChangedEventArgs e)
if (e.Property == ValueProperty)
static void OnColorChannelChanged(DependencyObject Object,
DependencyPropertyChangedEventArgs args)
static public bool MyValidateValueCallback(object value)
return true;
int v = (int)(value);
if (v > 5)
return false;

return true;
private static object CoerceColorChannel(DependencyObject
element, object value)
int current = (int)value;
if (current < 0 )
current = 0;
else if (current > 5)
current = 5;
return current;

public override void OnApplyTemplate()
Button btn = Template.FindName("Button1", this) as Button;
btn.Click += new RoutedEventHandler(btn_Click);

void btn_Click(object sender, RoutedEventArgs e)
this.Value += 1;
The XAML below
<Window x:Class="WindowsApplication2.Window1"
Title="WindowsApplication2" Height="300" Width="300"

<ControlTemplate x:Key="CustomControl1ControlTemplate1"
TargetType="{x:Type WindowsApplication2:CustomControl1}">
<Border Background="{TemplateBinding Background}" x:Name="Border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<StackPanel x:Name="StackPanel">
<Button x:Name="Button1" Content="Button1"/>
<Label x:Name="Label" Content="{TemplateBinding Value}"/>

<WindowsApplication2:CustomControl1 x:Name="CustomControl1"
Template="{DynamicResource CustomControl1ControlTemplate1}"
Value="{Binding Value, ElementName=Slider, Mode=TwoWay,
<Label Margin="74.4,116,99.2,99.2" x:Name="Label"/>
<Slider VerticalAlignment="Bottom" Margin="36.8,0,36.8,63.2"
Height="29.6" x:Name="Slider"/>


My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)