2009-09-02 1 views
0

私はいくつかのデータソースにバインドするにはどうすればよいカスタムクラスをWPFでバインド可能にするにはどうすればいいですか?

internal class PageInformation : DependencyObject 
{ 
    public static DependencyProperty NameProperty = 
     DependencyProperty.Register("Name", typeof(string), typeof(PageInformation)); 

    public static DependencyProperty PageUriProperty = 
     DependencyProperty.Register("PageUri", typeof(string), typeof(PageInformation)); 

    public string Name 
    { 
     get; 
     set; 
    } 

    public Uri PageUri 
    { 
     get; 
    } 
} 

、クラスを持っていますか?

私の考えは、アプリケーション内のすべてのページに関する情報を格納するXMLファイルを持ち、このクラスを<Page.Resources>にしてXMLファイルにバインドすることです。

XMLファイルには、次のようになります。私が持っていると思います

<Elements> 
     <Element Name="Administration" DisplayName="Administration" Value="1" PageUri="Administration.xaml" > 
      <Element Name="Categories" DisplayName="Categories" Value="2" PageUri="Administration.xaml" ></Element> 
      <Element Name="Goals" DisplayName="Goals" Value="3" PageUri="Administration.xaml" ></Element> 
      <Element Name="Settings" DisplayName="Settings" Value="4" PageUri="Administration.xaml" ></Element> 
     </Element> 
</Elements> 

は次のようにXAMLを持っている:

<Page.Resources> 
      <local:PageInformation 
       x:Key="pageInfo" 
       Name="{Binding XPath=/Elements/Element[@Name='Administration']}" 
       Source="/samples.xml" > 
      </local:PageInformation> 
    </Page.Resources> 

を私はNameプロパティの値を持っている場合は、私が移入するためのコードを書きたいです他のプロパティも(おそらくデータコンテキストを使用して?)。

どうすればいいですか? Nameプロパティの変更コードを実行するには

答えて

0

、依存関係プロパティの登録にPropertyChangedCallbackを指定します。

public static DependencyProperty NameProperty = 
    DependencyProperty.Register("Name", typeof(string), typeof(PageInformation), 
    new FrameworkPropertyMetadata(OnNameChanged)); 

private static void OnNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    // do whatever you want here 
} 

あなたOnNameChangedハンドラは、PageInformationの他のプロパティを移入することができます。

他の WPF要素が名前の変更に応答して更新されるようにするには、そのプロパティをPageInformationのNameプロパティにバインドすることで行います。

関連する問題