2011-08-15 16 views
0

私は、xaml-filesランタイムをロードしてランタイムデータにバインドしたいと考えていました。私はこのような「PropertySetter」と呼ばれるコンポーネントを使用することになり、これらのXAML-ファイル内 :ランタイムにバインドされたXAML

public class PropertySetter : UserControl 
{ 
    public string Value { get; set; } 
    public string Text { get; set; } 
    public string Adress { get; set; } 

    public PropertySetter() 
    { 
    } 
} 

そしてxamlfile、次のようになります。

<Grid 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:vf="clr-namespace:VisualFactory.vfGraphics.Graphics;assembly=VisualFactory"> 
    <vf:PropertySetter Name="Datapoint" Text="propset" Value="false" Adress="10201" /> 
    <CheckBox IsChecked="{Binding ElementName=Datapoint, Path=Value}" Content="{Binding ElementName=Datapoint, Path=Text}" /> 
</Grid> 

私はあなたが今ではポイントを得る願っています。 このチェックボックスは、値(content、ischecked)をプロパティセットにバインドし、アプリケーションが実行されると、単に "PropertySetter"の束の値を変更し、グラフィックスはその内容と値を更新します。 しかし、xamlはロード時に値を正しく設定していますが、プロパティセットの値を変更すると値が更新されません。私はMode = TwoWayを設定しようとしており、propertysettersはiNotifyコレクションに入っています。 誰もこれの前に何かを試したことがありますか?

答えて

0

答えは "たDependencyProperty" クラスに産みます。

public class PropertySetter : UserControl 
    { 
     public string Value 
     { 
      get { return (string)GetValue(ValueProperty); } 
      set { SetValue(ValueProperty, value); } 
     } 

     public static readonly DependencyProperty ValueProperty = 
      DependencyProperty.Register("Value", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("0")); 




     public string Text 
     { 
      get { return (string)GetValue(TextProperty); } 
      set { SetValue(TextProperty, value); } 
     } 

     public static readonly DependencyProperty TextProperty = 
      DependencyProperty.Register("Text", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("def")); 




     public string Adress 
     { 
      get { return (string)GetValue(AdressProperty); } 
      set { SetValue(AdressProperty, value); } 
     } 

     public static readonly DependencyProperty AdressProperty = 
      DependencyProperty.Register("Adress", typeof(string), typeof(PropertySetter), new UIPropertyMetadata("")); 



     public PropertySetter():base() 
     { 
     } 
    } 

を出来上がり、それはすべて正常に結合し、私は今、実行時にロードXAMLとアプリ・ロジックとの間で層を作った:私はこれにPropertySetterを変更しました。

0

ここで行う必要があるのは、PropertySetterクラスにINotifyPropertyChangedを実装し、各プロパティの設定者にPropertyChangedイベントを発生させることです。これは、バインディングがソースの変更時にUIを更新することを知るためのメカニズムです。

class PropertySetter :UserControl,INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private string address; 
    private string text; 
    private string value; 

    public string Value 
    { 
     get { return value; } 
     set 
     { 
      if (value == value) 
       return; 
      value = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Value")); 
     } 
    } 

    public string Text 
    { 
     get { return text; } 
     set 
     { 
      if (text == value) 
       return; 
      text = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Text")); 
     } 
    } 

    public string Address 
    { 
     get { return address; } 
     set 
     { 
      if (address == value) 
       return; 
      address = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Address")); 
     } 
    } 

    public PropertySetter() 
    { 

    } 
} 

PS:TwoWayデータバインディングのため、それは変更がターゲット(あなたのケースでは、CheckBoxのプロパティ)に行われた場合、それらの中に反映されていることをデータバインディングを伝えるために使用されていますソース(あなたの場合は、PropertySetterのプロパティ)。

希望はこのことができます:)

+0

こんにちはAbdouThankが答えましたが、私はすでに役に立たなかったのです。 – Filip

+0

私はここで答えを見つけました:http://www.wpftutorial.net/DependencyProperties.html#creation 私はちょうどpropertysetterクラスのDependencyProperty – Filip

+0

ああ! XAMLの値を設定していて、質問を読んで忘れてしまっただけで、変更の通知をしていないことが分かりましたので、これは本当に問題です。しかし、なぜXAMLで作成される 'PropertySetter'が必要なのでしょうか(私もそのコメントを追加しようとしていました) – AbdouMoumen

関連する問題