C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,513 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,943 people online right now. Registration is fast and FREE... Join Now!




WPF User Control propert binding

 

WPF User Control propert binding, Problems binding to a custom property

ragingben

29 Jun, 2009 - 06:15 AM
Post #1

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 153



Thanked: 9 times
Dream Kudos: 150
My Contributions
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: 13

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:17AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month