views:

367

answers:

1

Hi there,

How do i extend an existing control (ComboBox in my case) to include a new property which i can bind to a property on my view model??

I have a Dependancy Property on the control's class as follows:

public class MyComboBox : ComboBox
{
    public static readonly DependencyProperty MyTextProperty =
        DependencyProperty.Register("MyText", typeof(string), typeof(MyComboBox));

    public string MyText
 {
  get
  {
   return GetValue(MyComboBox.MyTextProperty).ToString();
  }

  set
  {
   SetValue(MyComboBox.MyTextProperty, value);    
  }
 }

And want to bind to it declaratively from XAML like this:

<MyComboBox MyText="{Binding MyTextOnViewModel,
    UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

The Binding just won't work, any ideas why??

Thanks.

+2  A: 

Your getter and setter reference TestTextProperty while the property is declared as MyTextProperty.

Your getter should also be casting instead of calling .ToString()

return (string)GetValue(MyTextProperty);

See this page for more complete instructions.

Bryce Kahle
your first points a typo, sorry, ill check the rest out now
andrej351
ok all good. thanks for the help man, but i had my custom control in a cell tamplate in a datagrid, which i forgot to mention.Just changed the binding to {Binding DataContext.MyTextOnViewModel, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Pages:MyPage}}}
andrej351