Note that there are some explanatory texts on larger screens.

plurals
  1. PONumeric UpDown control using dependency properties
    text
    copied!<p>So I'm at a loss to understand this. I have a custom control that uses up/down buttons to increase/decrease the time as displayed on toggle buttons. I have the <code>Content</code> property of the Toggles displaying the correct values of the <code>Hour</code> and <code>Minute</code> properties, and I'm now trying to set up the code-behind to increase the values. According to the VS2010 debugger, it <em>is</em> increasing the value of the Hour property, but it's not changing the content of the Toggles to reflect this. I have the binding mode set to <code>TwoWay</code>, and I'm using {Binding <code>RelativeSource={RelativeSource TemplatedParent}, Path=Hour, Mode=TwoWay}</code> to bind to the value as this is within the <code>generic.xaml</code> file of the custom control. Any ideas on how to make the displayed value update correctly?</p> <p><strong>XAML:</strong> (Style templates removed to save space)</p> <pre><code> &lt;Style TargetType="{x:Type local:TimePicker}"&gt; &lt;Setter Property="Height" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}" /&gt; &lt;Setter Property="Width" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}" /&gt; &lt;Setter Property="HorizontalAlignment" Value="Center" /&gt; &lt;Setter Property="VerticalAlignment" Value="Center" /&gt; &lt;Setter Property="FontSize" Value="14" /&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate&gt; &lt;Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="1"&gt; &lt;StackPanel x:Name="PART_Root" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="{TemplateBinding VerticalAlignment}"&gt; &lt;!--Region Hour Button--&gt; &lt;ToggleButton x:Name="PART_Hour" VerticalAlignment="{TemplateBinding VerticalAlignment}" Margin="0" BorderBrush="Transparent" BorderThickness="0" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Hour, Mode=TwoWay, BindsDirectlyToSource=True}"&gt; &lt;/ToggleButton&gt; &lt;!--EndRegion--&gt; &lt;Label HorizontalContentAlignment="{TemplateBinding HorizontalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalAlignment}" FontSize="14" Content=":"/&gt; &lt;!--Region Minute Button--&gt; &lt;ToggleButton x:Name="PART_Minute" VerticalAlignment="{TemplateBinding VerticalAlignment}" Margin="0" BorderBrush="Transparent" Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Minute}"&gt; &lt;/ToggleButton&gt; &lt;!--EndRegion--&gt; &lt;StackPanel x:Name="PART_IncDecPanel" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical"&gt; &lt;Grid Height="{Binding ElementName=PART_Hour, Path=ActualHeight}"&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="*"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;Viewbox Stretch="Fill" Grid.Row="0"&gt; &lt;!--Region Increase Button--&gt; &lt;Button x:Name="PART_IncreaseTime" HorizontalContentAlignment="{TemplateBinding HorizontalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalAlignment}" BorderBrush="Transparent" BorderThickness="0" FontFamily="Marlett" Foreground="DimGray" Content="5" Padding="0" Click="PART_IncreaseTime_Click"&gt; &lt;/Button&gt; &lt;!--EndRegion--&gt; &lt;/Viewbox &gt; &lt;Viewbox Stretch="Fill" Grid.Row="1"&gt; &lt;!--Region Decrease Button--&gt; &lt;Button x:Name="PART_DecreaseTime" HorizontalContentAlignment="{TemplateBinding HorizontalAlignment}" VerticalContentAlignment="{TemplateBinding VerticalAlignment}" BorderBrush="Transparent" BorderThickness="0" FontFamily="Marlett" Foreground="DimGray" Content="6" Padding="0"&gt; &lt;/Button&gt; &lt;!--EndRegion--&gt; &lt;/Viewbox&gt; &lt;/Grid&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p><strong>Code:</strong></p> <pre><code>public class TimePicker : Control { #region Dependency Property Declarations public static DependencyProperty HourProperty = DependencyProperty.Register("Hour", typeof(int), typeof(TimePicker), new FrameworkPropertyMetadata((int)12, new PropertyChangedCallback(OnHourChanged))); public static DependencyProperty MinuteProperty = DependencyProperty.Register("Minute", typeof(string), typeof(TimePicker), new FrameworkPropertyMetadata((string)"00", new PropertyChangedCallback(OnMinuteChanged))); #endregion #region Properties public int Hour { get { return (int)GetValue(HourProperty); } set { SetValue(HourProperty, value); } } public string Minute { get { return (string)GetValue(MinuteProperty); } set { SetValue(MinuteProperty, value); } } #endregion #region Events private static void OnHourChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { TimePicker time = new TimePicker(); time.Hour = (int)e.NewValue; MessageBox.Show("Hour changed"); } private static void OnMinuteChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { } #endregion static TimePicker() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TimePicker), new FrameworkPropertyMetadata(typeof(TimePicker))); } } public partial class TimePickerEvents : ResourceDictionary { TimePicker time = new TimePicker(); void PART_IncreaseTime_Click(object sender, RoutedEventArgs e) { time.Hour += 1; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload