Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suggest using a converter to achieve what you want. </p> <p>Your user control's XAML will look like this:</p> <pre><code>&lt;UserControl x:Class="MyDateControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:MyDateControl" x:Name="root"&gt; &lt;UserControl.Resources&gt; &lt;my:DatePartConverter x:Key="DatePartConverter" Date="{Binding ElementName=root, Path=Date}"/&gt; &lt;/UserControl.Resources&gt; &lt;StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBox Name="textbox_year" Text="{Binding ElementName=root, Path=Date, Converter={StaticResource DatePartConverter}, ConverterParameter=year, Mode=TwoWay}"/&gt; &lt;TextBox Name="textbox_month" Text="{Binding ElementName=root, Path=Date, Converter={StaticResource DatePartConverter}, ConverterParameter=month, Mode=TwoWay}" /&gt; &lt;TextBox Name="textbox_day" Text="{Binding ElementName=root, Path=Date, Converter={StaticResource DatePartConverter}, ConverterParameter=day, Mode=TwoWay}" /&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>In code-behind you will have only you dependency property:</p> <pre><code>public DateTime Date { get { return (DateTime)GetValue(DateProperty); } set { SetValue(DateProperty, value); } } public static readonly DependencyProperty DateProperty = DependencyProperty.Register("Date", typeof(DateTime), typeof(MyDateControl), new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); </code></pre> <p>And the converter will look something like this:</p> <pre><code>public class DatePartConverter : Freezable, IValueConverter { public DateTime Date { get { return (DateTime)GetValue(DateProperty); } set { SetValue(DateProperty, value); } } public static readonly DependencyProperty DateProperty = DependencyProperty.Register("Date", typeof(DateTime), typeof(DatePartConverter), new UIPropertyMetadata(DateTime.Now)); public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DateTime date = (DateTime)value; string datePartType = (string)parameter; string result; switch (datePartType) { case "year": result = date.Year.ToString().PadLeft(4, '0'); break; case "month": result = date.Month.ToString().PadLeft(2, '0'); break; case "day": result = date.Day.ToString().PadLeft(2, '0'); break; default: throw new InvalidOperationException("Unknown date part type (ConverterParameter)"); } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { string datePartValue = (string)value; string datePartType = (string)parameter; DateTime result; switch (datePartType) { case "year": result = new DateTime(int.Parse(datePartValue), Date.Month, Date.Day); break; case "month": result = new DateTime(Date.Year, int.Parse(datePartValue), Date.Day); break; case "day": result = new DateTime(Date.Year, Date.Month, int.Parse(datePartValue)); break; default: throw new InvalidOperationException("Unknown date part type (ConverterParameter)"); } return result; } protected override Freezable CreateInstanceCore() { return new DatePartConverter(); } } </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