![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | 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 | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| wpf xml twoway data binding problem? | .NET General | |||
| BUG: Redirecting function contents to a file truncates function lines at the width of the console | PowerShell | |||