Categories
askquestion

How to make custom class bindable in WPF?

How to make custom class bindable in WPF?

Ask Question

Asked
10 years, 6 months ago

Active
8 years, 7 months ago

Viewed
599 times

.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}

0

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?

wpf data-binding

share|improve this question

edited Aug 9 ’11 at 17:53

Dave Clemmer

3,6871111 gold badges4646 silver badges7272 bronze badges

asked Sep 2 ’09 at 4:36

VijayVijay

2,09344 gold badges2626 silver badges3737 bronze badges

add a comment
 | 

1 Answer
1

Active

Oldest

Votes

0

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.

share|improve this answer

answered Sep 2 ’09 at 5:12

itowlsonitowlson

68.3k1313 gold badges147147 silver badges147147 bronze badges

add a comment
 | 

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Submit

Post as a guest

Name

Email
Required, but never shown

Post Your Answer

Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged wpf data-binding or ask your own question.

The Overflow Blog

Defending yourself against coronavirus scams

Podcast Episode 220: Fully Remote

Featured on Meta

Planned maintenance scheduled for Saturday, March 28, 2020 at 13:00 UTC (9AM…

An Update On Creative Commons Licensing

Community and Moderator guidelines for escalating issues via new response…

How does the Triage queue work?

Triage needs to be fixed urgently, and users need to be notified upon…

Related

574How do I use WPF bindings with RelativeSource?180Binding a WPF ComboBox to a custom list165Binding to static property2WPF change Button Content on ViewModel.PropertyChanged event1DependencyProperty.Register() usage?1946How does data binding work in AngularJS?0Set Visibility of control based on custom DependencyProperty0WPF can’t set Name of custom Textbox in Usercontrol

Hot Network Questions

Does lower gravity on Mars make it unsafe and unhealthy for humans?

How to get return value from helper class to controller class?

Why is the effect called Dodge?

How much of the US media referred to the COVID-19 as the “Wuhan Virus” until it received an official designation from the WHO?

Why do ETFs that replicate the S&P 500 vary significantly in price?

How do “tip jet” helicopters cancel the torque effect of the main rotor?

Football Rivals

Since every human has a different DNA (different combinations of C, G, A, T) what does it mean to have the genome done?

What’s the point of Super Star Destroyers?

Animating 1D Convection-Diffusion Equation to reach steady state

I have a YMA Visa – will Germany let me into the country despite COVID-19?

Any Implementation in C++

What is Austria?

Is it true that Netscape Navigator eventually became Mozilla Firefox?

Why would a ‘first world’ alien opt to have a brood of dumb children rather than one smart one?

Is there a name for the great circle where latitude and longitude are equal?

Why not 7.Qa4+ in Carlsen vs Navara QGD

Where would you keep a goat that is living in a manor?

How could a person correct their vision with limited technology?

Assign [email protected] to another variable in a shell function

Why is water running into my water heater when cold water inlet is turned off?

How to make bread with plain flour?

Can I substitute salts 1:1 by weight?

Identifying the subject of a sentence

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

default

Leave a Reply

Your email address will not be published. Required fields are marked *