我有一个可以绑定到的属性的UserControl。此属性需要更新UserControl UI。UserControl有两个文本块,属性需要用字符串的一半更新一个文本块,用另一半更新另一个文本块。
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>UserControl CodeBehind (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()方法。但是,使用databind不能。
bAny反馈将不胜感激。
谢谢
发布于 2016-06-16 01:55:41
Ryan Flohr的答案可以做你想做的事,但既然他提到了长缠绕法,我想我也会提出长缠绕法。长线法绝对是推荐的方法。
代码背后:
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), New PropertyMetadata(New PropertyChangedCallback(AddressOf RawStringPropertyChanged)))
Public Property Rawstring As String
Get
Return GetValue(RawStringProperty)
End Get
Set(value As String)
SetValue(RawStringProperty, value)
End Set
End Property
Private Shared Sub RawStringPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim control As HexView = CType(d, HexView)
control.Parse()
End Sub
Public Shared ReadOnly ParsedStringOneProperty As DependencyProperty = DependencyProperty.Register("ParsedStringOne", GetType(String), GetType(HexView), New PropertyMetadata(String.Empty))
Public Property ParsedStringOne As String
Get
Return GetValue(ParsedStringOneProperty)
End Get
Set(value As String)
SetValue(ParsedStringOneProperty, value)
End Set
End Property
Public Shared ReadOnly ParsedStringTwoProperty As DependencyProperty = DependencyProperty.Register("ParsedStringTwo", GetType(String), GetType(HexView), New PropertyMetadata(String.Empty))
Public Property ParsedStringTwo As String
Get
Return GetValue(ParsedStringTwoProperty)
End Get
Set(value As String)
SetValue(ParsedStringTwoProperty, value)
End Set
End Property
Private Sub Parse()
ParsedStringOne = Rawstring.Substring(0, Rawstring.Length / 2)
ParsedStringTwo = Rawstring.Substring(Rawstring.Length / 2)
End Sub
End ClassXAML:
<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"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HexView}},Path=ParsedStringOne}"/>
<TextBlock x:Name="txtTwo" Width="100" Height="100"
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="120,10,0,0"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HexView}},Path=ParsedStringTwo}" />
</Grid>
</UserControl>发布于 2016-06-16 01:13:55
当您使用绑定访问依赖项属性时," get“和"Set”后面的代码实际上不会被调用。get'r和set'r只是"GetValue()“和"SetValue()”的包装器,便于代码隐藏使用。
我对您的问题的简短回答是,下面的代码更改至少可以使其以当前形式工作:
利用依赖项属性上的PropertyChangedCallback委托,让它调用"Parse()“方法。
Public Shared ReadOnly RawStringProperty As DependencyProperty = DependencyProperty.Register("RawString", GetType(String), GetType(HexView), New PropertyMetadata(New PropertyChangedCallback(AddressOf RawStringPropertyChanged)))
Public Property Rawstring As String
Get
Return GetValue(RawStringProperty)
End Get
Set(value As String)
SetValue(RawStringProperty, value)
End Set
End Property
Private Shared Sub RawStringPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim control As HexView = CType(d, HexView)
control.Parse()
End Sub,我对你问题的正确回答是:。
虽然这是合法的,但通常应避免在代码隐藏中按名称引用控件。对于这两个字符串,您应该有每个字符串的依赖项属性,然后将文本框绑定到它们。
我希望这能帮到你!
发布于 2016-06-16 02:25:00
编写一个IValueConverter,它将为您完成工作。
class ParseStringConv : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter.ToString() == "Left")
return value.ToString().Substring(0, value.ToString().Length / 2);
return value.ToString().Substring(value.ToString().Length / 2);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}用法:
<TextBlock Text="{Binding Name, Converter={StaticResource ParseConv}, ConverterParameter='Left'}" />
<TextBlock Text="{Binding Name, Converter={StaticResource ParseConv}, ConverterParameter='Right'}" />传递适当的ConverterParameter。
https://stackoverflow.com/questions/37846474
复制相似问题