依赖项属性上的XAML绑定 [英] XAML Binding on Dependency Property

查看:86
本文介绍了依赖项属性上的XAML绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用WPF TimePicker控件. 该控件继承了一个TextBox,并具有一个MaskedTexProvider,该MaskedTexProvider以以下格式显示TimeSpan:

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"

到目前为止,一切都按预期进行(上下箭头更改了基础TimeSpan的小时和分钟等).

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

我在将我的TimePicker控件的TimeSpan属性绑定到TimeSpan对象方面遇到问题.

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

如果我手动设置时间属性(它公开了底层的TimeSpan对象),它会起作用,但是当我尝试通过XAML设置时间属性时,它就不会起作用...

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...

例如,以下工作原理:

 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>

这是我的TimePicker的时间属性"实现.

Here is my TimePicker's Time Property implementation.

Public Class TimePicker
    Inherits TextBox
    Implements INotifyPropertyChanged

    Public Shared TimeSpanProperty As DependencyProperty = DependencyProperty.Register("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

我在做什么错了?

事实证明我有一个组合 的问题阻止了 工作的约束力.

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

首先,我不应该 为我使用私有的TimeSpan成员 财产.我应该一直在使用 GetValue()和SetValue()方法 改为设置DependencyProperty.

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.

第二,我没有关注 的命名约定 DependencyProperty.应该有 紧随其后的是时间"属性名称 通过财产"(换句话说, 应该已经命名为TimeProperty).

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).

第三,我需要使用 FrameworkPropertyMetadata类型为 指定当 属性已更改.这是我的地方 放置用于设置文本的逻辑 TimePicker控件.

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.

我发现的大多数信息 对寻找解决方案最有帮助 在这个MSDN中发现我的问题 文章:自定义依赖项 属性

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

感谢您的帮助!

-Frinny

推荐答案

使用DependencyProperty时,需要使用它的实现而不是INotifyPropertyChanged样式的属性实现.这意味着没有后备字段(通过DP机制处理),并且使用属性更改后的回调进行更改处理或值强制,而不是在属性设置器中进行操作.

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.

我建议查看MSDN上的依赖项属性概述 .特别是,您需要查看如何实现依赖项属性以及回调和验证.

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.

这篇关于依赖项属性上的XAML绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆