How to use a dependency property in the same user control?

Advertisements

I have a user control that defines a dependecy property to can use communicate with another user control.

This dependency propperty is in the code behind.

In this user control I have a combobox, from which I want to notify the selected item to the second user control.

The code of the first user control:

public static readonly DependencyProperty TipoComponenteSeleccionadoProperty =
            DependencyProperty.Register("TipoComponenteSeleccionado", typeof(TiposComponentes),
            typeof(ucClasificacionesComponentesBaseView), new PropertyMetadata(null));
        public bool TipoComponenteSeleccionado
        {
            get
            {
                return (bool)GetValue(TipoComponenteSeleccionadoProperty);
            }
            set
            {
                SetValue(TipoComponenteSeleccionadoProperty, value);
            }
        }

I was trying something like that in the xaml:

<ComboBox Name="cmbTiposComponentes" Width="150"
          TipoComponenteSeleccionado="{Binding ElementName=cmbTiposComponentes, Path=SelectedItem}">

The idea is when I select the item in the combobox, update the dependency property, so the second user control can bind it and be notified.

But I get an errror because the dependency property can’t be used in the combobox.

So I was wondering if there is some way to use the dependency property in the combobox.

I have tried to define a static resource in the xaml of the first user control, something like that:

<UserControl.Resources>
    <local:MyMainUserControl.DependencyProperty Key.../>
</UserControl.Resources>

But the itellisense shows me all the views that I have except this, so I don’t have access to the depedency property of the view.

So is it possible to use the dependency property in the view in which it is defined?

Thanks.

>Solution :

bind SelectedItem to TipoComponenteSeleccionado:

<ComboBox Name="cmbTiposComponentes" Width="150"
          SelectedItem="{Binding Path=TipoComponenteSeleccionado, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type local:TiposComponentes}}}">

Leave a ReplyCancel reply