views:

251

answers:

1

I'm trying to get a better understanding of what dependency properties and what they are not. I've built the example below which enables a combobox's choices to change based on how the user moves a slider.

In creating this I learned that dependency properties actually have nothing to do with INotifyPropertyChanged as is used in ViewModel properties, which simplified the below example.

But now how would I go from this example below to recreating the kind of dependency property seen in DockPanel.Dock="Top", e.g. so I could enable the following kind of XAML use:

<local:ExtendedComboBox 
    Margin="5 5 5 0"
    DataIdCode="{Binding ElementName=TheSource, Path=Value}">
    <Image local:ExtendendedComboBox="Left" ... />
    <TextBlock local:ExtendendedComboBox="Right" ... />
</local:ExtendedComboBox>

Is this possible? And is this the same kind of use of dependency properties as in the more straight-forward example below, or is this, like INotifyPropertyChanged, yet another kind of binding technology in WPF?

Here is the slider/combobox example:

XAML:

<Window x:Class="TestDependency9202.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestDependency9202"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <StackPanel
            Margin="5 5 5 0"
            Orientation="Horizontal">
            <TextBlock Text="Customers"
                       Margin="0 0 3 0"/>
            <Slider x:Name="TheSource"
                HorizontalAlignment="Left"
                Value="0"
                Width="50"
                SnapsToDevicePixels="True"
                Minimum="0"
                Margin="0 0 3 0"
                Maximum="1"/>
            <TextBlock Text="Employees"/>
        </StackPanel>
        <local:ExtendedComboBox 
            Margin="5 5 5 0"
            DataIdCode="{Binding ElementName=TheSource, Path=Value}"/>
    </StackPanel>
</Window>

Code-Behind:

using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace TestDependency9202
{
    public partial class ExtendedComboBox : ComboBox
    {
        public static readonly DependencyProperty DataIdCodeProperty =
            DependencyProperty.Register("DataIdCode", typeof(string), typeof(ExtendedComboBox),
                new PropertyMetadata(string.Empty, OnDataIdCodePropertyChanged));

        private static void OnDataIdCodePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            ExtendedComboBox extendedComboBox = dependencyObject as ExtendedComboBox;
            extendedComboBox.OnDataIdCodePropertyChanged2(e);
        }

        private void OnDataIdCodePropertyChanged2(DependencyPropertyChangedEventArgs e)
        {
            if (DataIdCode == "0")
            {
                Items.Clear();
                Items.Add("customer1");
                Items.Add("customer2");
                Items.Add("customer3");
            }
            else if (DataIdCode == "1")
            {
                Items.Clear();
                Items.Add("employee1");
                Items.Add("employee2");
                Items.Add("employee3");
            }
            this.SelectedIndex = 0;
        }


        public string DataIdCode
        {
            get { return GetValue(DataIdCodeProperty).ToString(); }
            set { SetValue(DataIdCodeProperty, value); }
        }

        public ExtendedComboBox()
        {
            InitializeComponent();
        }
    }

}
+2  A: 

This kind of dependency property is called Attached Property. That's basically a dependency property that is used on another object. You're using DependencyProperty.RegisterAttached to create them, and provide two static methods for getting and setting them. See the above link.

Julien Lebosquain