tags:

views:

152

answers:

1

I have a class,

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;
    }
}

How can I bind it to some data source?

My idea is to have an XML file which stores the information about all the pages in the application, have this class in <Page.Resources> and bind it to the XML file.

The XML file looks like this:

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

I want to have have the XAML like this:

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

When I have value for Name property, I want to write code to populate other properties as well (probably by using the data context?).

Can somebody tell me how can I achieve this?

A: 

To run code when the Name property changes, specify a PropertyChangedCallback in the dependency property registration:

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
}

Your OnNameChanged handler can then populate other properties of the PageInformation.

Note that if you are wanting other WPF elements to update in response to the Name change, you would do that by binding their properties to the PageInformation's Name property.

itowlson