【发布时间】:2016-06-15 21:56:01
【问题描述】:
我有一个带有可以绑定属性的 UserControl。此属性需要更新 UserControl UI。 UserControl 有两个文本块,属性需要用一半的字符串更新一个文本块,用另一半更新另一个文本块。
用户控件 XAML:
<UserControl x:Class="HexView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:LearningWPF"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock x:Name="txtOne" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0">Hola</TextBlock>
<TextBlock x:Name="txtTwo" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="120,10,0,0">Adios</TextBlock>
</Grid>
</UserControl>
用户控制代码隐藏 (VB)
Imports System.ComponentModel
Public Class HexView
Private s_Rawstring As String
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Shared ReadOnly RawStringProperty As DependencyProperty = DependencyProperty.Register("RawString", GetType(String), GetType(HexView))
Public Property Rawstring As String
Get
Return GetValue(RawStringProperty)
End Get
Set(value As String)
SetValue(RawStringProperty, value)
Parse()
End Set
End Property
Private Sub Parse()
txtOne.Text = Rawstring.Substring(0, Rawstring.Length / 2)
txtTwo.Text = Rawstring.Substring(Rawstring.Length / 2)
End Sub
End Class
如果我将属性设置为
hexview.rawstring = "This is a sample property"
UserControlUI 已更新,因为它使用 setter 访问器并执行 Parse() 方法。但是使用数据绑定不会。
b任何反馈将不胜感激。
谢谢
【问题讨论】:
标签: wpf vb.net data-binding user-controls