Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

Keep depency properties in sync

Closed Thread
 
Thread Tools Display Modes
Old 04-20-2006   #1 (permalink)
Weifen Luo
Guest


 

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.
Old 04-26-2006   #2 (permalink)
Weifen Luo
Guest


 

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.

Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing Option in Sync Center Compared to Active Sync lowercasep Vista hardware & devices 0 02-20-2008 10:45 AM
After Vista PPC laptop sync no Windows 2000 desktop sync? RJ Vista General 0 01-23-2008 09:52 AM
Cannot access properties in "local area connection properties" win Samzabrus Vista networking & sharing 2 10-09-2007 10:00 AM
View sync status before perform the sync actions with Vista offlinefiles Chau Chee Yang Vista file management 0 08-01-2007 01:54 AM
Sync Center won't sync to Windows Mobile PDA/cell phone Bruce Wilkinson Vista General 5 03-13-2007 04:16 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50