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

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

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




WPF Binding on user control breaking

 

WPF Binding on user control breaking, ARGH!!! Why do you only work one way!

ragingben

3 Jul, 2009 - 03:17 AM
Post #1

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 153



Thanked: 9 times
Dream Kudos: 150
My Contributions
Hi everyone, so I have this code for a user control, which has a slider which I use for updating whatever is bound to it. When I change the source the slider updates no probs, all fine. However if i use the slider not only does it not update the source, but it also seems to break the binding as updating the source then has no effect on the slider or Value property! WHAT IS GOING ON HERE?!

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;
using RoToRHDA2WPF.UIClases;
using System.ComponentModel;

namespace RoToRHDA2WPF.UserControls
{
    /// <summary>
    /// Interaction logic for ParamaterModControl.xaml
    /// </summary>
    public partial class ParamaterModControl : UserControl
    {
        #region Fields

        /// <summary>
        /// Get or set the button style
        /// </summary>
        public Style ButtonStyle
        {
            get { return (Style)GetValue(ButtonStyleProperty);; }
            set { SetValue(ButtonStyleProperty, value); }
        }

        /// <summary>
        /// Get or set the slider style
        /// </summary>
        public Style SliderStyle
        {
            get { return (Style)GetValue(SliderStyleProperty); }
            set { SetValue(SliderStyleProperty, value); }
        }

        /// <summary>
        /// Get or set the units
        /// </summary>
        public String Units
        {
            get { return (String)GetValue(UnitsProperty);; }
            set { SetValue(UnitsProperty, value); }
        }

        /// <summary>
        /// Get or set the max value
        /// </summary>
        public Double Max
        {
            get { return (Double)GetValue(MaxProperty); }
            set { SetValue(MaxProperty, value); }
        }

        /// <summary>
        /// Get or set the min value
        /// </summary>
        public Double Min
        {
            get { return (Double)GetValue(MinProperty); }
            set { this.SetValue(MinProperty, value); }
        }

        /// <summary>
        /// Get or set the title
        /// </summary>
        public String Title
        {
            get { return (String)GetValue(TitleProperty); }
            set { this.SetValue(TitleProperty, value); }
        }

        /// <summary>
        /// Get or set the default value associated with this control
        /// </summary>
        public Double DefaultValue
        {
            get { return (Double)GetValue(DefaultValueProperty); }
            set { this.SetValue(DefaultValueProperty, value); }
        }

        /// <summary>
        /// Get or set the display image
        /// </summary>
        public ImageSource DisplayImage
        {
            get { return (ImageSource)GetValue(DisplayImageProperty); }
            set { SetValue(DisplayImageProperty,value); }
        }

        /// <summary>
        /// Get or set the tick frequency
        /// </summary>
        public Double TickFrequency
        {
            get { return (Double)GetValue(TickFrequencyProperty);; }
            set { SetValue(TickFrequencyProperty, value); }
        }

        /// <summary>
        /// Get or set the value this control represents
        /// </summary>
        public Double Value
        {
            get { return (Double)GetValue(ValueProperty); }
            set { this.SetValue(ValueProperty,value); }
        }

        /// <summary>
        /// Get or set the tool tip for the image
        /// </summary>
        public String DisplayImageToolTip
        {
            get { return (String)GetValue(DisplayImageToolTipProperty); }
            set { SetValue(DisplayImageToolTipProperty, value); }
        }

        /// <summary>
        /// Occurs when the value changes
        /// </summary>
        public event RoutedPropertyChangedEventHandler<Double> ValueChanged;

        /// <summary>
        /// Occurs when default is clicked
        /// </summary>
        public event RoutedEventHandler DefaultClicked;

        #endregion

        #region DependancyProperties

        /// <summary>
        /// Dependancy property for this controls value
        /// </summary>
        public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(Double), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnValuePropertyChanged)));
        /// <summary>
        /// Dependancy property for controls min value
        /// </summary>
        public static DependencyProperty MinProperty = DependencyProperty.Register("Min", typeof(Double), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(ParamaterModControl.OnMinPropertyChanged)));
        /// <summary>
        /// Dependancy property for controls max value
        /// </summary>
        public static DependencyProperty MaxProperty = DependencyProperty.Register("Max", typeof(Double), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(ParamaterModControl.OnMaxPropertyChanged)));
        /// <summary>
        /// Dependancy property for this controls default value
        /// </summary>
        public static DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(Double), typeof(ParamaterModControl));
        /// <summary>
        /// Dependancey property for this controls title
        /// </summary>
        public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(String), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnTitlePropertyChanged)));
        /// <summary>
        /// Dependancy property for controls units value
        /// </summary>
        public static DependencyProperty UnitsProperty = DependencyProperty.Register("Units", typeof(String), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnUnitsPropertyChanged)));
        /// <summary>
        /// Dependacey property for controls button style
        /// </summary>
        public static DependencyProperty ButtonStyleProperty = DependencyProperty.Register("ButtonStyle", typeof(Style), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnButtonStylePropertyChanged)));
        /// <summary>
        /// Dependacey property for controls slider style
        /// </summary>
        public static DependencyProperty SliderStyleProperty = DependencyProperty.Register("SliderStyle", typeof(Style), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnSliderStylePropertyChanged)));
        /// <summary>
        /// Dependacey property for tick frequency slider style
        /// </summary>
        public static DependencyProperty TickFrequencyProperty = DependencyProperty.Register("TickFrequency", typeof(Double), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnTickFrequencyPropertyChanged)));
        /// <summary>
        /// Dependacey property for display image
        /// </summary>
        public static DependencyProperty DisplayImageProperty = DependencyProperty.Register("DisplayImage", typeof(ImageSource), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnDisplayImagePropertyChanged)));
        /// <summary>
        /// Dependacey property for image tool tip
        /// </summary>
        public static DependencyProperty DisplayImageToolTipProperty = DependencyProperty.Register("DisplayImageToolTip", typeof(String), typeof(ParamaterModControl), new PropertyMetadata(new PropertyChangedCallback(OnDisplayImageToolTipPropertyChanged)));

        #endregion

        #region Methods

        /// <summary>
        /// Create a new parameter modifier control
        /// </summary>
        public ParamaterModControl()
        {
            InitializeComponent();
        }

        #endregion

        #region DependancyPropertyCallbacks

        /// <summary>
        /// The call back to image style changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnSliderStylePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set image
            ((ParamaterModControl)obj).valueSlider.Style = (Style)args.NewValue;
        }

        /// <summary>
        /// The call back to button style changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnButtonStylePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set image
            ((ParamaterModControl)obj).defaultButton.Style = (Style)args.NewValue;
        }

        /// <summary>
        /// The call back to display image changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnDisplayImageToolTipPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set image
            ((ParamaterModControl)obj).controlImage.ToolTip = args.NewValue.ToString();
        }

        /// <summary>
        /// The call back to display image changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnDisplayImagePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set image
            ((ParamaterModControl)obj).controlImage.Source = (ImageSource)args.NewValue;
        }

        /// <summary>
        /// The call back to tick frequency changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnTickFrequencyPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set frequency
            ((ParamaterModControl)obj).valueSlider.TickFrequency = (Double)args.NewValue;
        }

        /// <summary>
        /// The call back to title changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnTitlePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set text
            ((ParamaterModControl)obj).titleTextBlock.Text = args.NewValue.ToString();
        }

        /// <summary>
        /// The call back to units changed
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="args"></param>
        public static void OnUnitsPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            // set text
            ((ParamaterModControl)obj).unitsTextBlock.Text = args.NewValue.ToString();
        }

        /// <summary>
        /// The callback to value changed
        /// </summary>
        /// <param name="o"></param>
        /// <param name="args"></param>
        public static void OnValuePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            // create reference
            ParamaterModControl modControl = o as ParamaterModControl;

            // if subscribers to event
            if (modControl.ValueChanged != null)
            {
                // dispatch with args
                modControl.ValueChanged(modControl.valueSlider, new RoutedPropertyChangedEventArgs<double>((Double)args.OldValue, (Double)args.NewValue));
            }
        }

        /// <summary>
        /// The callback to max changed
        /// </summary>
        /// <param name="o"></param>
        /// <param name="args"></param>
        public static void OnMaxPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            // update
            ((ParamaterModControl)o).valueSlider.Maximum = (Double)args.NewValue;
        }

        /// <summary>
        /// The callback to min changed
        /// </summary>
        /// <param name="o"></param>
        /// <param name="args"></param>
        public static void OnMinPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            // update
            ((ParamaterModControl)o).valueSlider.Minimum = (Double)args.NewValue;
        }

        #endregion

        #region EventHandlers

        private void defaultButton_Click(object sender, RoutedEventArgs e)
        {
            // if subscribers
            if (this.DefaultClicked != null)
            {
                // send on the click
                this.DefaultClicked(this.defaultButton, e);
            }
            else
            {
                // set default
                this.valueSlider.Value = this.DefaultValue;
            }

        }

        private void controlImage_ToolTipOpening(object sender, ToolTipEventArgs e)
        {
            // set handled
            e.Handled = true;

            // create shout out
            ShoutOut so = new ShoutOut(this.DisplayImageToolTip, this.controlImage);
        }

        private void controlImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // set handled
            e.Handled = true;

            // if we gotta tool tip to display
            if (this.DisplayImageToolTip != null)
            {
                // create shout out
                ShoutOut so = new ShoutOut(this.DisplayImageToolTip, this.controlImage, 0, true);
            }
        }

        private void defaultButton_ToolTipOpening(object sender, ToolTipEventArgs e)
        {
            // set handled
            e.Handled = true;

            // create shout out
            ShoutOut so = new ShoutOut(String.Format("Default value for {0} {1} {2}", this.Title, this.DefaultValue, this.Units), this.defaultButton);
        }

        #endregion
    }
}



CODE

<UserControl x:Class="RoToRHDA2WPF.UserControls.ParamaterModControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="52" HorizontalAlignment="Stretch" VerticalAlignment="Top" Name="instance">
    <Grid Name="controlGrid" Margin="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="52"/>
            <ColumnDefinition Width="55"/>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="55"/>
            <ColumnDefinition Width="25*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="50*"/>
        </Grid.RowDefinitions>
        <Border Name="imageBorder" BorderBrush="Black" CornerRadius="2.5" BorderThickness="1" Width="50" Grid.RowSpan="2">
            <Image Name="controlImage" Margin="1" ToolTipOpening="controlImage_ToolTipOpening" MouseDown="controlImage_MouseDown"/>
        </Border>
        <Button Name="defaultButton" Content="Default" HorizontalAlignment="Stretch" Margin="6,1,6,1" Click="defaultButton_Click" Grid.Column="1" ToolTipOpening="defaultButton_ToolTipOpening" ToolTip="" />
        <TextBlock Name="titleTextBlock" Text="title" FontFamily="SwitzerLand" FontSize="15" Width="100" Grid.Column="2" TextWrapping="Wrap" VerticalAlignment="Center" Foreground="Black" />
        <TextBlock Name="valueTextBlock" FontFamily="SwitzerLand" Text="{Binding ElementName=valueSlider, Path=Value}" FontSize="15" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="3" Foreground="Black" />
        <TextBlock Name="unitsTextBlock" FontFamily="SwitzerLand" FontSize="15" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="units" Grid.Column="4" Foreground="Black" />
        <Slider TickPlacement="BottomRight" Name="valueSlider" Margin="6,0,6,0" HorizontalAlignment="Stretch" MinWidth="300" TickFrequency="1" SmallChange="{Binding RelativeSource={RelativeSource Self}, Path=TickFrequency}" LargeChange="{Binding RelativeSource={RelativeSource Self}, Path=TickFrequency}" Grid.Row="1" Grid.ColumnSpan="4" Grid.Column="1" IsSnapToTickEnabled="True" Value="{Binding ElementName=instance, Path=Value, Mode=TwoWay}"/>
    </Grid>
</UserControl>



And an example of usage...
CODE

<UserControls:ParamaterModControl x:Name="exampleControl" Title="Diameter:" Units="mm" Min="0" ButtonStyle="{StaticResource UITargetButton}" DisplayImage="/Images/PinningOptions/WetMixDiameter.png" DefaultValue="0.5" Value="{Binding Source={x:Static RoToRComponents:RoToR.LiquidMix}, Path=MixDiameterTarget, Converter={StaticResource ValueRounder}}" Max="{Binding Source={x:Static RoToRComponents:RoToR.LiquidMix}, Path=MaximumDiameterTarget}" TickFrequency="0.1" Margin="0,0,0,6" DisplayImageToolTip="The diameter of the mix applied to both the x and y axis"/>


Sorry the example is explicit to my program, but hopefully there is enough info there for someone in the know to set me on my way smile.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:23AM

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