2016-06-15 11 views
0

バインド可能なプロパティを持つUserControlがあります。このプロパティは、UserControl UIを更新する必要があります。 UserControlには2つのテキストブロックがあり、そのプロパティは1つのテキストブロックを文字列の半分で更新し、もう1つのテキストブロックを残りの半分で更新する必要があります。バインディングプロパティで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> 

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" 

のようにプロパティを設定した場合、それはセッターaccesorを使用しているためUserControlUIが更新されるとメソッドParse()を実行します。ただし、databindを使用していません。

bフィードバックは高く評価されます。あなたは「取得」と、実際に呼び出されません。「設定」の背後にあるバインディング、コードを使用して依存関係プロパティにアクセスすると

はあなたに

答えて

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> 
+1

これは良い答えですが、小さなバグがあります。 xamlでは、コードビハインドのDependency Propertyにバインドするので、BindingをRelativeSourceとして指定する必要があります。そうでない場合は、動作しません。 –

+0

私はあなたの答えを編集し、StackOverflowは誰かがそれを見直したらそれが見えると言った。 –

+1

私はそれを忘れてしまいました、ありがとう:D – Jai

2

ありがとうございます。 get'rおよびset'rは、コードビハインドの使用の便宜のために、単に「GetValue()」および「SetValue()」を包むラッパーです。

あなたの質問への私の短い答えは、少なくとも、それが現在の形で働いて取得するために、次のコード変更である:あなたの依存関係プロパティの

活用PropertyChangedCallbackデリゲートをし、それが呼び出す必要があり、「解析を()」方法。

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つの文字列については、それぞれに依存プロパティを設定し、テキストボックスをそれらにバインドする必要があります。

こちらがお役に立てば幸いです。

+0

PropertyChangeCallbackを持つソリューションが機能します。みんなありがとう。AltControl、UserControlのUIのコントロールを参照するのは何ですか?なぜそれは、コントロールの依存関係プロパティを使用してより良いaproachですか?また、私は2つのテキストブロックで問題を単純化してもらいましたが、現実には私は256について話しています...アプローチが変わりますか?ありがとうございます –

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を渡します。

+0

私の謙虚な意見では、私はこの答えが質問の中心的な問題になるような気がしません。この問題は、DependencyPropertyの設定時に呼び出される「Parse」メソッドがあることですが、この「Parse」メソッドは、DependencyPropertyがバインディングではなくコードを使用して設定されている場合にのみ呼び出されます。 ValueConverterの使い方はここではわかりません。 –

+0

@RyanFlohrこの特殊なケースでは、コンバーターを使用するのが正しい方法です。 – AnjumSKhan

+0

実際はうまくいくでしょう。 @RyanFlohr これにより、Parse()メソッドを使用する必要がなくなります。 – Jai

関連する問題