0

I have a problem with binding a property of a custom control of me to a Value of my mainform. I already used the DependencyProperties and it works in an old control of mine, but in the current control it doesn't work and I have no idea why not. My current code for the property of my form with the value, that should be binded to the custom control:

    private ObservableCollection<ConnectionItem> _ConList = new ObservableCollection<ConnectionItem>();
    public ObservableCollection<ConnectionItem> ConList 
    {
        get { return _ConList; }
        set
        {
            _ConList = value;
            triggerPropertyChanged("ConList");
        }
    }

the XAML-Code for this looks like this:

<Fluent:RibbonWindow 
    x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Fluent="clr-namespace:Fluent;assembly=Fluent"
    Title="Test"      
    Height="600"
    Width="800"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    >
    <Grid>
        <aTreeView Name="tvConList" Items="{Binding ConList}" />
    </Grid>
</Fluent:RibbonWindow>

My Custom-Control has this code:

public partial class aTreeView : INotifyPropertyChanged
{
    public aTreeView()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register(
            "Items",
            typeof(ObservableCollection<ConnectionItem>),
            typeof(aTreeView),
            new PropertyMetadata(new ObservableCollection<ConnectionItem>())
            );

    private ObservableCollection<ConnectionItem> _Items;

    /// <summary>
    /// Gets the current Items
    /// </summary>
    public ObservableCollection<ConnectionItem> Items
    {
        get
        {
            return ((ObservableCollection<ConnectionItem>)GetValue(ItemsProperty));
            //return _Items;
        }
        set
        {
            SetValue(ItemsProperty, value);
            //_Items = value;                
        }
    }
}

With this code, there are not compiling-Failures (except I did Copy-Paste-mistakes) but the get/set of the Items-Property of the Control will never executed, if I set some breakpoints to this.

Hunv
  • 385
  • 7
  • 17
  • If you mean the getter and setter of `Items` property, it's hard to believe that the execution could not jump to there. At least the XAML code `Items="{Binding ConList}"` should instruct it to do so. Also I noticed that your `aTreeView` class does not inherit from `DependencyObject`, it may be a missing while copying/pasting (because I see the `GetValue` and `SetValue` are used inside that class). – King King Oct 18 '14 at 20:12
  • 1
    If you set a dependency property in XAML, WPF directly calls SetValue, effectively skipping the CLR wrapper method. See [XAML Loading and Dependency Properties](http://msdn.microsoft.com/en-us/library/bb613563.aspx) for an explanation. – Clemens Oct 18 '14 at 20:19
  • @KingKing The class does not inherit from DependencyObject, that is correct. That is, because it already inherits from UserControl (by XAML - not shown, sorry). I cannot inherit from UserControl and DependencyObject as far as I know. – Hunv Oct 18 '14 at 22:25
  • Hi, It was late yesterday, but because of UserControl inherits from DependencyObject, so there is an inherit of DependencyObject. http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol(v=vs.110).aspx – Hunv Oct 19 '14 at 08:03
  • OK, I got it. This Post helped me. I removed the DataContext and replaced it by the x:Name-Property. Without any problems, its working now. http://stackoverflow.com/a/22247870/1406475 – Hunv Oct 19 '14 at 17:34

0 Answers0