patterncsharpMinor
WPF DateTimePicker user control
Viewed 0 times
wpfdatetimepickercontroluser
Problem
I've created a
XAML:
XAML.CS
`public partial class DateTimePicker : UserControl, INotifyPropertyChanged
{
public DateTimePicker()
{
InitializeComponent();
(Content as FrameworkElement).DataContext = this;
}
#region SelectedTime
public DateTime? SelectedDateTime
{
get
{
return (DateTime?)GetValue(SelectedDateTimeProperty);
}
set
{
SetValue(SelectedDateTimeProperty, value);
}
}
public static readonly DependencyProperty
SelectedDateTimeProperty =
DependencyProperty.Register("SelectedDateTime",
typeof(DateTime?),
typeof(DateTimePicker));
#endregion
private bool _isPopupOpen;
public bool IsPopupOpen
{
get { return _isPopupOpen; }
set
{
SetProperty(ref _isPopupOpen, value);
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected virtual void SetProperty(ref T member, T va
UserControl that should act as a control inside a Window:XAML:
XAML.CS
`public partial class DateTimePicker : UserControl, INotifyPropertyChanged
{
public DateTimePicker()
{
InitializeComponent();
(Content as FrameworkElement).DataContext = this;
}
#region SelectedTime
public DateTime? SelectedDateTime
{
get
{
return (DateTime?)GetValue(SelectedDateTimeProperty);
}
set
{
SetValue(SelectedDateTimeProperty, value);
}
}
public static readonly DependencyProperty
SelectedDateTimeProperty =
DependencyProperty.Register("SelectedDateTime",
typeof(DateTime?),
typeof(DateTimePicker));
#endregion
private bool _isPopupOpen;
public bool IsPopupOpen
{
get { return _isPopupOpen; }
set
{
SetProperty(ref _isPopupOpen, value);
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
protected virtual void SetProperty(ref T member, T va
Solution
You have used two types of bindings:
This binding is fine and is the way how WPF binding system was supposed to work with respect to the MVVM pattern.
The interactions between xaml controls and xaml code-behind are not supposed to flow through the same MVVM pattern. This is why in the partial class you have the possibility to access directly your controls.
Example from xaml:
Example from code-behind:
Another drawback could be the usage of some properties from outside of your implementation. Suppose there is some logic which needs to open the popup without a click on the
- Binding between your control and the usage DataContext
This binding is fine and is the way how WPF binding system was supposed to work with respect to the MVVM pattern.
- Bindings between xaml controls and the xaml class (Same class which is the parent of those controls)
The interactions between xaml controls and xaml code-behind are not supposed to flow through the same MVVM pattern. This is why in the partial class you have the possibility to access directly your controls.
Example from xaml:
Example from code-behind:
void ButtonChangeStatus_OnClick(object sender, RoutedEventArgs e)
{
IsPopupOpen = !IsPopupOpen;
ButtonChangeStatus.Background = Brushes.Green;
}Another drawback could be the usage of some properties from outside of your implementation. Suppose there is some logic which needs to open the popup without a click on the
ButtonChangeStatus button but from a click which originates outside of your implementation. To achieve such behavior the user of your control would need to bind IsOpen to his DataContext. But in the current implementation it's not possible without a great confusion (you need to have two IsOpen dependency properties); it will be possible by exposing the IsOpen dependency property to the outside ViewModel in the same way as you already did with SelectedDateTime. Having both a local IsOpen property and a dependency one will allow you to handle the Popup using two sources.Code Snippets
<Button Grid.Row="0"
Grid.Column="1"
Width="50"
BorderBrush="Orange"
BorderThickness="1"
Name="ButtonChangeStatus"
Click="ButtonChangeStatus_OnClick"
Style="{DynamicResource SquareButtonStyle}">void ButtonChangeStatus_OnClick(object sender, RoutedEventArgs e)
{
IsPopupOpen = !IsPopupOpen;
ButtonChangeStatus.Background = Brushes.Green;
}Context
StackExchange Code Review Q#98905, answer score: 3
Revisions (0)
No revisions yet.