views:

22

answers:

3

so here i come creating a user control. it consists of a treeview dropping down from a combobox. actually there is a button with a control (DropTree) dropping down from it's contextmenu. so i have an control DropTree

public partial class DropTree : UserControl
{
    public TreeView TreeView 
    { get{return treeView;} }

    public DropTree()
    { InitializeComponent(); }
}

to simplify it i made the TreeView control public than i have my main control which is called ComboTreeView

now i need to represent some treeview properties in it, so i define several dependency properties:

    public static DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(ComboTreeView), new FrameworkPropertyMetadata { Inherits = true, IsNotDataBindable = false, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });

    public object SelectedItem
    {
        get { return GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

and in constructor it is

public ComboTreeView()
{
            InitializeComponent();
            TreeViewControl.SetBinding(TreeView.SelectedItemProperty, new Binding("SelectedItem") { Source = this, Mode = BindingMode.TwoWay });
}

and it all seems ok, until i run it... it crushes saying that SelectedItem cannot be binded to data o_0 i don't get it?

the same goes for ItemsSource and SelectedValue... But only SelectedValuePath property defined this way goes fine...

can anybody help? or is there any other way to bind it correctly? PS: by the way, i need to use DataBinding for ComboTreeView in my code later

A: 

Can anybody help me ?)

A: 

Try to set the Binding on SelectedValue instead of SelectedItem.

Captain
A: 

TreeView.SelectedItem is a readonly property. You can't set it, whether explicitly or through binding. In order to select a node in a TreeView, you must set the TreeViewItem.IsSelected property to true.

Thomas Levesque