我有一个用于我的新应用程序的控件。这个控件本身就有一个常规属性。
Public Property Value() As String
Get
If AutoCompleteTextBox.SearchText Is Nothing Then
Return String.Empty
Else
Return AutoCompleteTextBox.SearchText.ToString.Trim
End If
End Get
Set(value As String)
AutoCompleteTextBox.SearchText = value
End Set
End Property编辑:
所以,经过多次尝试,我终于到了这个阶段。
Public Shared ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(String), GetType(AutoCompleteBox))
Public Property Value() As String
Get
Return Me.GetValue(ValueProperty).ToString
End Get
Set(value As String)
Me.SetValue(ValueProperty, value)
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged这是依赖属性。此属性仍不具有约束力。绑定的输出窗口中不显示任何错误。
Text="{Binding RelativeSource={RelativeSource Self}, Path=Value, Mode=TwoWay}"这是我的绑定方法。我不知道我还能做什么。至少如果有一个错误,我可以找出一些东西。没有任何错误,我在这里只是一只无头鸡。
发布于 2015-10-13 15:35:55
有关所有依赖项基础http://www.wpftutorial.net/dependencyproperties.html的信息,请参阅以下url
基本上,您可以通过提供一个FrameworkPropertyMetadata来获取依赖属性的属性更改事件。
new FrameworkPropertyMetadata( [Default Value],
OnCurrentTimePropertyChanged);您可以在事件处理程序中取回目标控件(DependencyObject),并在那里实现您的逻辑
private static void OnCurrentTimePropertyChanged(DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
AutoCompleteTextBox control = source as AutoCompleteTextBox;
string time = (string)e.NewValue;
// Put some update logic here...
}发布于 2015-10-13 15:42:04
在控件中声明依赖属性是一件好事。
您可以在xaml中进行一些绑定(对不起,我没有您的XAML -我想是这样的)。
类似于:
<TextBox x:Name="AutoCompleteTextBox"
Text="{Binding RelativeSource={RelativeSource=Self},Path=Value}"/>问候
发布于 2015-10-13 15:44:36
TextBox有一个名为Text的属性。当您访问文本属性时,它将为您提供在TextBox中输入的文本。你的情况也是如此。那么为什么要将其转换为DP呢?如果您想要将此DP绑定到某个其他控件,则DP将非常有用。
扩展此控件本身。创建一个新的控件并引入这个新的DP。
而要将此属性绑定到某个控件的地方则使用DP。然后,根据绑定模式设置,从控件更新此属性或从此DP更新控件。
如何进行绑定:
<TextBox x:Name="UserInput" />
<uc:MyAutoCompleteTextBox ValueDP="{Binding Text, ElementName=UserInput, Mode=OneWay}" />MyAutoCompleteTextBox是从旧的AutoComplete控件扩展(继承)的新控件。
如果您想要应用一些过滤逻辑或其他任何东西,您可以将其应用于DP本身,如下所示:
Get
someVariable = TryCast(Me.GetValue(ValueProperty), String)
' apply somg logic to someVariable
' use your old Value property from here
Return someVariable
End Getnet上有很多WPF绑定教程。
我推荐:http://blog.scottlogic.com/2012/04/05/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-one.html
https://stackoverflow.com/questions/33096475
复制相似问题