Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Custom dependency properties in InkCanvas can't work in design time

3 views
Skip to first unread message

astw

unread,
Feb 5, 2009, 1:47:14 AM2/5/09
to

Hi,
I inherited new class from InkCanvas, and define two dependency
properties PBWidth PBHeight which is bind respectively to the Width, and
Height properties. When add this new class to a window, and change the
PBWidth, or PBHeight property in the XAML file, the control's size can
not be changed, this is not what I expected. But in runtime, change the
PBHeight or PBWidth, the control's height or width can be changed.
What's wrong with it? I post the simplified code of the class here,
please help me to see what's wrong with it, thank you very much!

public class InkPicture :InkCanvas
{
public static readonly DependencyProperty PBHeightProperty =
DependencyProperty.Register(
"PBHeight",
typeof(short),
typeof(InkPicture),
new FrameworkPropertyMetadata((short)400, new
PropertyChangedCallback(OnPBHeightChanged)));

public static readonly DependencyProperty PBWidthProperty =
DependencyProperty.Register(
"PBWidth",
typeof(short),
typeof(InkPicture),
new FrameworkPropertyMetadata((short)480, new
PropertyChangedCallback(OnPBWidthChanged)));


public static readonly RoutedEvent HeightChangedEvent =
EventManager.RegisterRoutedEvent(
"PBHeightChanged",
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<short>),
typeof(InkPicture));

public static readonly RoutedEvent WidthChangedEvent =
EventManager.RegisterRoutedEvent(
"WidthChanged",
RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<short>),
typeof(InkPicture));

public InkPicture()
{

this.SetPropertylBinding("PBHeight",
InkPicture.HeightProperty);
this.SetPropertylBinding("PBWidth",
InkPicture.WidthProperty);
}


public short PBHeight
{
get
{
return (short)GetValue(PBHeightProperty);
}

set
{
this.SetValue(PBHeightProperty, value);
}
}

public short PBWidth
{
get
{
return (short)GetValue(PBWidthProperty);
}

set
{
SetValue(PBWidthProperty, value);
}
}

private static void OnPBHeightChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
InkPicture control = obj as InkPicture;

RoutedPropertyChangedEventArgs<short> e = new
RoutedPropertyChangedEventArgs<short>(
(short)args.OldValue, (short)args.NewValue,
HeightChangedEvent);


MessageBox.Show("pbheight changed");

control.RaiseEvent(e);
}
private static void OnPBWidthChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs args)
{
InkPicture control = obj as InkPicture;

RoutedPropertyChangedEventArgs<short> e = new
RoutedPropertyChangedEventArgs<short>(
(short)args.OldValue, (short)args.NewValue,
WidthChangedEvent);

control.RaiseEvent(e);
}

private void SetPropertylBinding(string path,
DependencyProperty target)
{
Binding bind = new Binding();
bind.RelativeSource = new
RelativeSource(RelativeSourceMode.Self);
bind.Path = new PropertyPath(path);
bind.Mode = BindingMode.TwoWay;
bind.UpdateSourceTrigger =
UpdateSourceTrigger.PropertyChanged;
bind.ConverterCulture = new CultureInfo("en-US");
SetBinding(target, bind);
}
}


--
astw

0 new messages