使用绑定属性更新UserControl UI

时间:2016-06-15 21:56:01

标签: wpf vb.net data-binding user-controls

我有一个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()。但是使用数据绑定不会。

b非常感谢。

谢谢

3 个答案:

答案 0 :(得分:2)

使用Binding访问依赖项属性时,&#34; Get&#34;和&#34; Set&#34;实际上并没有被召唤。 get&#39; r和set&r; r只是&#34; GetValue()&#34;的包装器。和&#34; SetValue()&#34;作为代码隐藏使用的便利。

我对你的问题的简短回答是以下代码更改,至少让它以当前形式运行:

利用您的Dependency属性上的PropertyChangedCallback委托,让它调用&#34; Parse()&#34;方法

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

我对你的问题的正确答案有点啰嗦:

虽然它是合法的,但通常应避免在代码隐藏中按名称引用控件。对于这两个字符串,您应该为每个字符串都有依赖项属性,然后将文本框绑定到它们。

我希望这有帮助!

答案 1 :(得分:2)

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 Class

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

答案 2 :(得分:0)

写一个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