【问题标题】:XAML Binding on Dependency Property依赖属性上的 XAML 绑定
【发布时间】:2011-06-25 04:11:21
【问题描述】:

我目前正在开发 WPF TimePicker 控件。 该控件继承了一个 TextBox,它有一个 MaskedTexProvider,它以以下格式显示 TimeSpan:

“时:分”

到目前为止,一切都按预期工作(向上和向下箭头更改底层 TimeSpan 的小时和分钟等)。

我在将 TimePicker 控件的 TimeSpan 属性绑定到 TimeSpan 对象时遇到问题。

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

例如,以下工作:

 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

但是,如果我尝试执行以下操作,则不会调用我的 Time 属性的“Set”:

<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 的时间属性实现。

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

我做错了什么?

编辑:

原来我有一个组合 的问题是阻止 从工作中绑定。

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

其次,我没有遵循 的命名约定 依赖属性。它应该有 一直是“时间”属性名称 由“财产”(换句话说 应该命名为 TimeProperty)。

第三,我需要使用 FrameworkPropertyMetadata 类型为 指定一个方法,当 属性变了。这是我的地方 把设置文本的逻辑 TimePicker 控件。

我找到的大部分信息 最有助于找到解决方案 在这个 MSDN 中发现了我的问题 文章:Custom Dependency Properties

感谢您的帮助!

-弗林尼

【问题讨论】:

    标签: xaml wpf-controls binding


    【解决方案1】:

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

    我建议查看 MSDN 上的 Dependency Property Overview 了解详细信息。特别是,您需要查看how to implement a Dependency PropertyCallbacks and Validation

    【讨论】:

    猜你喜欢
    • 2014-01-09
    • 2023-03-13
    • 2017-12-18
    • 2017-03-04
    • 2011-12-20
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    相关资源
    最近更新 更多