Printable Version of Topic

Click here to view this topic in its original format

Dream.In.Code _ C# _ WPF User Control propert binding

Posted by: ragingben 29 Jun, 2009 - 06:15 AM

Hi there, I have created a WPF usercontrol and added some properties, which when I set in XAML update on the control as expected. However my control has a slider which I want to bind to in the designer when I add the control. I have added a dependancy property but am having no luck with the binding! Please help if you know the answer.

Below is the code and the project, zipped up

WPF control

CODE

<UserControl x:Class="WPFBindingExample.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="150" Width="300" BorderBrush="Red" BorderThickness="2">
    <Grid Name="controlGrid" Background="Yellow">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <Slider Name="slider" VerticalAlignment="Center" TickPlacement="BottomRight"/>
        <Label Name="valueLabel" Grid.Row="1" Content="{Binding ElementName=slider, Path=Value}"/>
    </Grid>
</UserControl>

CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFBindingExample
{
    /// <summary>
    /// Interaction logic for TestControl.xaml
    /// </summary>
    public partial class TestControl : UserControl
    {
        /// <summary>
        /// Get or set the max value
        /// </summary>
        public Double Max
        {
            get { return this.slider.Maximum; }
            set { this.slider.Maximum = value; }
        }

        /// <summary>
        /// Get or set the min value
        /// </summary>
        public Double Min
        {
            get { return this.slider.Minimum; }
            set { this.slider.Minimum = value; }
        }

        /// <summary>
        /// Get or set the tick frequency
        /// </summary>
        public Double TickFrequency
        {
            get { return this.slider.TickFrequency; }
            set { this.slider.TickFrequency = value; }
        }

        /// <summary>
        /// Get or set the value this control represents
        /// </summary>
        public Double Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        /// <summary>
        /// Get or set the value this control represents
        /// </summary>
        private Double value;

        /// <summary>
        /// Register proerty
        /// </summary>
        public static DependencyProperty ValueBindingProperty = DependencyProperty.Register("Value", typeof(Double), typeof(TestControl));


        public TestControl()
        {
            InitializeComponent();
        }
    }
}


And the implemtation XAML
CODE

<Window x:Class="WPFBindingExample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:this="clr-namespace:WPFBindingExample"
    Title="Window1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="200"/>
        </Grid.RowDefinitions>
        <Slider Name="valueSimulatorSlider" Value="5" Minimum="2" Maximum="10" TickPlacement="BottomRight"/>
        <this:TestControl x:Name="testControl1" Max="5" Min="1" TickFrequency="0.5" Grid.Row="1" Value="{Binding ElementName=valueSimulatorSlider, Path=Value}"/>
    </Grid>
</Window>


This is not my actual control I am trying to create, but a simplified version to demo my problem.


Attached File(s)
Attached File  WPFBindingExample.zip ( 89.74k ) Number of downloads: 15

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)