开发者

XAML Binding on Dependency Property

开发者 https://www.devze.com 2023-02-09 16:22 出处:网络
I am currently working on a WPF TimePicker Control. The control inherits a TextBox and it has a MaskedTexProvider which displays a TimeSpan in the following format:

I am currently working on a WPF TimePicker Control. The control inherits a TextBox and it has a MaskedTexProvider which displays a TimeSpan in the following format:

"HH:MM"

So far everything is working as expected (up and down arrows change the hours and minutes of the underlying TimeSpan etc).

I am having problems with respect to Binding the TimeSpan Property of my TimePicker Control to a TimeSpan object.

It works if I manually set the Time Property (which exposes the underlying TimeSpan object), but not when I try to set the Time Property through XAML...

For example, the following works:

 Private Sub Test_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        TimeSpan.TryParse("2:30", myTimePicker.Time)
 End Sub

However if I try doing something like the following, my Time Property's "Set" does not get called:

<Window x:Class="Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:systhreading="clr-namespace:System.Threading;assembly=mscorlib"
        xmlns:myNS="clr-namespace:myNS"
        Title="Login" Height="768" Width="1024">
    <Window.Resources>
        <myNS:TestClass x:Key="myTestingClass"></myNS:TestClass>
    </Window.Resources>
    <DockPanel DataContext="{Binding Source={StaticResource myTestingClass}}">
            <myNS:TimePicker x:Name="myTimePicker" Time="{Binding TheTimeSpan}"></myNS:TimePicker>
    </DockPanel>
</Window>

Here is my TimePicker's Time Property implementation.

Public Class TimePicker
    Inherits TextBox
    Implements INotifyPropertyChanged

    Public Shared TimeSpanProperty As DependencyProperty = DependencyProperty.Reg开发者_JS百科ister("Time", GetType(TimeSpan), GetType(TimePicker))
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    Private _timeSpan As TimeSpan

    Public Property Time As TimeSpan
        Get
            Return _timeSpan
        End Get
        Set(ByVal value As TimeSpan)
            _timeSpan = value
            Dim str As String = _timeSpan.Hours.ToString.PadLeft(2, "0"c) + ":" + _timeSpan.Minutes.ToString.PadLeft(2, "0"c)
            Me.Text = str
            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("Time"))
        End Set
    End Property
    '..... the rest of the class implementation '
End Class

What am I doing wrong?

Edit:

It turns out that I had a combination of problems that was preventing the binding from working.

First of all, I should not have been using a private TimeSpan member for my property. I should have been using the GetValue() and SetValue() methods to set the DependencyProperty instead.

Secondly, I had not followed the naming conventions for the DependencyProperty. It should have been the "Time" property name followed by the "Property" (in other words it should have been named TimeProperty).

Thirdly, I needed to use a FrameworkPropertyMetadata type to specify a method to call when the property is changed. This is where I put the logic for setting the text of the TimePicker control.

Most of the information that I found most helpful in finding the solution to my problem was found in this MSDN Article: Custom Dependency Properties

Thanks for your help!

-Frinny


When you use a DependencyProperty, you need to use it's implementation instead of your INotifyPropertyChanged style implementation for properties. This means no backing field (it's handled via the DP mechanisms), and using property changed callbacks for change handling or value coersion instead of doing it in the property setter.

I recommend looking at the Dependency Property Overview on MSDN for details. In particular, you'll need to look at how to implement a Dependency Property as well as Callbacks and Validation.

0

精彩评论

暂无评论...
验证码 换一张
取 消