自定义控件的绑定属性

时间:2014-10-18 18:50:05

标签: c# wpf xaml

我将自定义控件的属性绑定到我的mainform的值时出现问题。我已经使用了DependencyProperties并且它在我的旧控件中工作,但在当前控件中它不起作用,我不知道为什么不。 我当前的表单属性的代码,其值应该绑定到自定义控件:

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

XAML-Code的代码如下:

<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>

我的自定义控件有以下代码:

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;                
        }
    }
}

使用此代码,没有编译 - 失败(除了我做了Copy-Paste-errors),但是如果我为此设置了一些断点,则控件的Items-Property的get / set将永远不会执行。

0 个答案:

没有答案