![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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. Step------------------------------------ 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 FrameworkPropertyMetadata(typeof(CustomControl1))); } 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 ValidateValueCallback(MyValidateValueCallback)); protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(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() { base.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" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication2" Height="300" Width="300" xmlns:WindowsApplication2="clr-namespace:WindowsApplication2" > <Window.Resources> <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}"/> </StackPanel> </Border> </ControlTemplate> </Window.Resources> <Grid> <WindowsApplication2:CustomControl1 x:Name="CustomControl1" Template="{DynamicResource CustomControl1ControlTemplate1}" Value="{Binding Value, ElementName=Slider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <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"/> </Grid> </Window> |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| TwoWay DataBinding to property having private set | gauravgoyal.aqua | Avalon | 4 | 04-03-2008 04:17 AM |
| Databinding issue | IHaveThePower | Avalon | 0 | 11-02-2007 07:02 AM |
| Databinding problem | black_nm | Avalon | 0 | 07-03-2007 05:35 AM |
| How to implement databinding like this? | Leo Xue | Avalon | 1 | 11-14-2006 03:38 PM |
| BUG: Redirecting function contents to a file truncates function lines at the width of the console | Adam Milazzo | PowerShell | 2 | 08-11-2006 10:58 PM |