首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用绑定属性更新UserControl UI

使用绑定属性更新UserControl UI
EN

Stack Overflow用户
提问于 2016-06-15 21:56:01
回答 3查看 335关注 0票数 0

我有一个可以绑定到的属性的UserControl。此属性需要更新UserControl UI。UserControl有两个文本块,属性需要用字符串的一半更新一个文本块,用另一半更新另一个文本块。

UserControl XAML:

代码语言:javascript
复制
<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)

代码语言:javascript
复制
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

如果我将属性设置为

代码语言:javascript
复制
hexview.rawstring = "This is a sample property"

UserControlUI是更新的,因为它使用了setter访问器,并执行了Parse()方法。但是,使用databind不能。

bAny反馈将不胜感激。

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-16 01:55:41

Ryan Flohr的答案可以做你想做的事,但既然他提到了长缠绕法,我想我也会提出长缠绕法。长线法绝对是推荐的方法。

代码背后:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2016-06-16 01:13:55

当您使用绑定访问依赖项属性时," get“和"Set”后面的代码实际上不会被调用。get'r和set'r只是"GetValue()“和"SetValue()”的包装器,便于代码隐藏使用。

我对您的问题的简短回答是,下面的代码更改至少可以使其以当前形式工作:

利用依赖项属性上的PropertyChangedCallback委托,让它调用"Parse()“方法。

代码语言:javascript
复制
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

,我对你问题的正确回答是:

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

我希望这能帮到你!

票数 2
EN

Stack Overflow用户

发布于 2016-06-16 02:25:00

编写一个IValueConverter,它将为您完成工作。

代码语言:javascript
复制
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();
        }
    }

用法:

代码语言:javascript
复制
<TextBlock Text="{Binding Name, Converter={StaticResource ParseConv}, ConverterParameter='Left'}" />

<TextBlock Text="{Binding Name, Converter={StaticResource ParseConv}, ConverterParameter='Right'}" />

传递适当的ConverterParameter

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37846474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档