![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| | Keep depency properties in sync In the WinFX SDK ColorPicker sample (http://windowssdk.msdn.microsoft.com...sp?frame=true), a MultiBinding object is created to keep the Color, Red, Green, Blue dependency properties in sync: private void SetupColorBindings() { MultiBinding binding = new MultiBinding(); binding.Converter = new ByteColorMultiConverter(); binding.Mode = BindingMode.TwoWay; Binding redBinding = new Binding("Red"); redBinding.Source = this; redBinding.Mode = BindingMode.TwoWay; binding.Bindings.Add(redBinding); Binding greenBinding = new Binding("Green"); greenBinding.Source = this; greenBinding.Mode = BindingMode.TwoWay; binding.Bindings.Add(greenBinding); Binding blueBinding = new Binding("Blue"); blueBinding.Source = this; blueBinding.Mode = BindingMode.TwoWay; binding.Bindings.Add(blueBinding); this.SetValue(ColorProperty, ((MarkupExtension)binding).ProvideValue(this, ColorProperty)); } I assume the last line can be replaced with: SetBinding(ColorProperty, binding); which is much more readable. However, this sync can be easily broken by the consumer of assigning another binding to ColorPicker.Color property: simply change the Window1.xaml as following: <Window x:Class="ColorPickerApp.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ColorPickerApp" xmlns:cp="clr-namespace:ColorPickerLib;assembly=ColorPickerLib"> <Grid> <StackPanel HorizontalAlignment="Left" Margin="20"> <StackPanel.Resources> <Color x:Key="MyColor">Blue</Color> </StackPanel.Resources> <cp:ColorPicker Name="colorPicker" Color="{Binding Source={StaticResource MyColor}}"/> </StackPanel> </Grid> </Window> It seems the actual instance binding object (BindingExpression or whatever) can only be attached to a dependency property; and setting binding on the denpendency property will overwrite the previous one. What is the appropriate way to keep the dependency properties in sync (override OnPropertyChange does not sound a good idea either)? In my opinion, the internal state of the class should never be broken by the consumer of the class. |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Keep depency properties in sync Just got confirmation from Kevin Moore (Program Manager of WPF), this is just for simplicity and in a shipping control, one should not write this way. "Weifen Luo" wrote: > In the WinFX SDK ColorPicker sample > (http://windowssdk.msdn.microsoft.com...sp?frame=true), > a MultiBinding object is created to keep the Color, Red, Green, Blue > dependency properties in sync: > > private void SetupColorBindings() > { > MultiBinding binding = new MultiBinding(); > > binding.Converter = new ByteColorMultiConverter(); > binding.Mode = BindingMode.TwoWay; > > Binding redBinding = new Binding("Red"); > redBinding.Source = this; > redBinding.Mode = BindingMode.TwoWay; > binding.Bindings.Add(redBinding); > > Binding greenBinding = new Binding("Green"); > greenBinding.Source = this; > greenBinding.Mode = BindingMode.TwoWay; > binding.Bindings.Add(greenBinding); > > Binding blueBinding = new Binding("Blue"); > blueBinding.Source = this; > blueBinding.Mode = BindingMode.TwoWay; > binding.Bindings.Add(blueBinding); > > this.SetValue(ColorProperty, > ((MarkupExtension)binding).ProvideValue(this, ColorProperty)); > } > > I assume the last line can be replaced with: > SetBinding(ColorProperty, binding); > which is much more readable. > > However, this sync can be easily broken by the consumer of assigning another > binding to ColorPicker.Color property: simply change the Window1.xaml as > following: > <Window x:Class="ColorPickerApp.Window1" > xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" > xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" > Title="ColorPickerApp" > xmlns:cp="clr-namespace:ColorPickerLib;assembly=ColorPickerLib"> > <Grid> > <StackPanel HorizontalAlignment="Left" Margin="20"> > <StackPanel.Resources> > <Color x:Key="MyColor">Blue</Color> > </StackPanel.Resources> > <cp:ColorPicker Name="colorPicker" Color="{Binding Source={StaticResource > MyColor}}"/> > </StackPanel> > </Grid> > </Window> > > It seems the actual instance binding object (BindingExpression or whatever) > can only be attached to a dependency property; and setting binding on the > denpendency property will overwrite the previous one. What is the appropriate > way to keep the dependency properties in sync (override OnPropertyChange does > not sound a good idea either)? In my opinion, the internal state of the class > should never be broken by the consumer of the class. |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Modifying OEM properties listed in system properties | Vista installation & setup | |||
| After Vista PPC laptop sync no Windows 2000 desktop sync? | Vista General | |||
| Cannot access properties in "local area connection properties" win | Vista networking & sharing | |||
| View sync status before perform the sync actions with Vista offlinefiles | Vista file management | |||
| Sync Center won't sync to Windows Mobile PDA/cell phone | Vista General | |||