更新具有绑定属性的UserControl UI [英] Update UserControl UI with a binding property

查看:109
本文介绍了更新具有绑定属性的UserControl UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以绑定到的属性的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 / $
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
xmlns:local =clr-namespace:LearningWPF
mc:Ignorable =d
d:DesignHeight =300d:DesignWidth =300>
< Grid>
< TextBlock x:Name =txtOneWidth =100Height =100Horizo​​ntalAlignment =LeftVerticalAlignment =TopMargin =10,10,0,0> Hola& TextBlock>
< TextBlock x:Name =txtTwoWidth =100Height =100Horizo​​ntalAlignment =LeftVerticalAlignment =TopMargin =120,10,0,0> TextBlock>

< / Grid>
< / UserControl>

UserControl CodeBehind(VB)

  Imports System.ComponentModel 

公共类HexView

私人s_Rawstring作为字符串

Public Sub New()

'这个调用是设计者需要的。
InitializeComponent()

'在InitializeComponent()调用之后添加任何初始化。
End Sub

公共共享ReadOnly RawStringProperty作为DependencyProperty = DependencyProperty.Register(RawString,GetType(String),GetType(HexView))
公共属性Rawstring As String
获取
返回GetValue(RawStringProperty)
结束Get
Set(value As String)
SetValue(RawStringProperty,value)
解析()
结束设置
结束属性

私有子解析()
txtOne.Text = Rawstring.Substring(0,Rawstring.Length / 2)
txtTwo.Text = Rawstring。 Substring(Rawstring.Length / 2)
End Sub
结束类

如果我设置的属性如

  hexview.rawstring =这是一个示例属性

UserControlUI被更新,因为它使用setter accesor并执行Parse()方法。但是使用databind不行。



bAny反馈将不胜感激。



谢谢

解决方案

Ryan Flohr 的答案会做你想要的,但是由于他提到了这种长期的方法,我想我也会采取这种长期的方法。长期以来的方法绝对是推荐的方法。



代码背后:

  Imports System.ComponentModel 

公共类HexView

私有s_Rawstring作为字符串

Public Sub New()

'这个调用是设计人员需要的。
InitializeComponent()

'在InitializeComponent()调用之后添加任何初始化。
End Sub

公共共享ReadOnly RawStringProperty作为DependencyProperty = DependencyProperty.Register(RawString,GetType(String),GetType(HexView),新的PropertyMetadata(New PropertyChangedCallback(AddressOf RawStringPropertyChanged)))
公共属性Rawstring As String
获取
返回GetValue(RawStringProperty)
结束Get
Set(value As String)
SetValue(RawStringProperty,value)
结束集
结束属性

私有共享Sub RawStringPropertyChanged(ByVal d As DependencyObject,ByVal e As DependencyPropertyChangedEventArgs)
Dim control As HexView = CType(d,HexView)
control.Parse()
End Sub

公共共享ReadOnly ParsedStringOneProperty作为DependencyProperty = DependencyProperty.Register(ParsedStringOne,GetType(String),GetType(HexView),New PropertyMetadata String.Empty))
公共属性ParsedStringOne As String
获取
返回GetValue(解析StringOneProperty)
End Get
Set(value As String)
SetValue(ParsedStringOneProperty,value)
结束集
结束属性

公共共享ReadOnly ParsedStringTwoProperty As DependencyProperty = DependencyProperty.Register(ParsedStringTwo,GetType(String),GetType(HexView),New PropertyMetadata(String.Empty))
公共属性ParsedStringTwo As String
获取
返回GetValue(ParsedStringTwoProperty)
结束Get
Set(value As String)
SetValue(ParsedStringTwoProperty,value)
结束集
结束属性


Private Sub Parse()
ParsedStringOne = Rawstring.Substring(0,Rawstring.Length / 2)
ParsedStringTwo = Rawstring.Substring(Rawstring.Length / 2)
End Sub
结束类

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 =300d:DesignWidth =300>
< Grid>
< TextBlock x:Name =txtOneWidth =100Height =100
Horizo​​ntalAlignment =LeftVerticalAlignment =TopMargin =10,10,0,0
Text ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type local:HexView}},Path = ParsedStringOne}/>
< TextBlock x:Name =txtTwoWidth =100Height =100
Horizo​​ntalAlignment =LeftVerticalAlignment =TopMargin =120,10,0,0
Text ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type local:HexView}},Path = ParsedStringTwo}/>

< / Grid>
< / UserControl>


I have a UserControl with a property that can be binded to. This property needs to update the UserControl UI. The UserControl has two textblocks and the property needs to update one textblockwith half of the string and the other textblock with the other half.

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

If I set the property like

hexview.rawstring = "This is a sample property"

the UserControlUI is updated because it uses the setter accesor and executes the method Parse(). However using databind does not.

bAny feedback will be appreciate.

Thank you

解决方案

Ryan Flohr's answer would do what you want, but since he mentioned the long-winded method, I guess I would put up the long-winded method as well. The long-winded method is definitely the recommended way to do it.

Code behind:

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>

这篇关于更新具有绑定属性的UserControl UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆