paulie13

Hi all,

I have written a custom control that derives from StackPanel and which has an attached property defined.

I have a window that uses the custom control and which contains a number of nested TextBox controls that have specified a value for this attached property.

My problem is that if I get access to one of these TextBox instances through code, is there any way to determine what attached properties are set on it (if any)

I have the usual "Get..." and "Set..." methods defined in the custom control but do not have access to these when testing.

Any help would be greatly appreciated!

Thanks,

Paul




Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

lee d

did you try using DependencyObject.ReadLocalValue method to see if a particular property has been set . There are few code samples in this forum showing how to get all the dependency properties of a control




Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

paulie13

Thanks a milllion Lee

That worked






Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

DSorre

May I ask how you gained access to the textboxes programmatically

I'm having lots of trouble accessing generated checkboxes using code.

Here's what I have and am trying to do:

I have a WrapPanel and inside is a ItemsControl that has a DataTemplate with a CheckBox. So when I bind the Itemssource of the ItemControl I get a nice list of checkboxes the way I want. When checking a checkbox a corresponding value is saved in a cart like dealio. Now when the user closes the "window" (an actual popup layer rather) and comes back to it I want to see if the value of the checkbox is in my cart and if so check the checkbox programmatically.

I tried accessing the checkbox like this:

foreach (VwAllServices LI in IC_Services.Items)

{

//If in cart

{

ContentPresenter pres = new ContentPresenter();

pres = (ContentPresenter)(IC_Services.ItemContainerGenerator.ContainerFromItem(LI));

DataTemplate dt = pres.ContentTemplate;

CheckBox checkBox = (CheckBox)dt.LoadContent();

checkBox.IsChecked = true;

}

}

That's not working and it's driving me nuts. I wish there was some resource that helped you out and showed how to programmatically access generated items in a Datatemplate...

Any ideas





Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

Neil Mosafi

That kind of programming is not really encouraged within WPF, the reason being that if you decide to change the DataTemplate you'll have to go and change loads of code aswell.

Why not add a boolean property to whatever it is you are binding your collection to which indicates whether the item is used within the cart. You can then bind the IsChecked to that boolean




Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

DSorre

Neil, Thanks for your quick response!

I was afraid of that answer actually, since I was browsing the web for quite some time and nobody really came across this issue before. To me it seemed so naturally, but that's probably because I come from a web programming background using scripting languages.

I'll try it the way you suggested, makes sense for sure.

Thanks again.





Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

Neil Mosafi

Well you can do it (I actually used to do a similar thing until I realised it wasn't great so refactored my app). If you *really* want to know, post some of your code here and I will show you.

Regards
Neil




Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

DSorre

Well now you got me to not wanting to do it that way :)

However if you would help me out on creating a collection that I can bind to I would really appreciate it. I have a query that was bound to the Itemscontrol, but the query result of course has no field for "ischecked" so I was thinking of pulling the query, creating a new Itemscollection and then looping through the recordset and adding each record to the collection. (in theory)

The problem I'm facing other than the fact that I've never done it before and know little about .NET is how to save three "columns" - ID, Name, IsChecked and appropriate values to then bind it to the itemscontrol

Does this make sense

Thanks so much for your help.





Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

Neil Mosafi

Hmm it depends how you are setting up your databinding really... can you post the definition of your ItemsSource as it stands now

Thanks




Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

DSorre

Well this is how I'm getting the data currently:

string FilterText = "Center_ID=" + Center_ID + " AND Category_ID=" + senderbutton.Content;

int TotalCount = 0;

//Get CenterServices

VwAllServicesService AllServicesService = new VwAllServicesService();

VList<VwAllServices> AllServices = AllServicesService.GetPaged(FilterText.ToString(), "Service_Name ASC", 0, 30, out TotalCount);

//Bind data to Itemscontrol

IC_Services.ItemsSource = AllServices;

You will notice that I'm using NetTiers and I can simply bind the VList to the Itemscontrol. Center_ID is just a variable to determine from which Center to get the services and the Category is driven by the category button you clicked.

Now I'd like to loop through the VList and put the data in a collection, just this time for every record I put in the collection I want to have a "column" isChecked and then have some logic to determine if it's true or false. I know how to loop through and also how to determine if it's true or false, the missing part is the collection or array (or whatever is best used in this scenario) which I need to build manually.

So in the end I am hoping to just say IC_Services.ItemsSource = MyNewManuallyCreatedCollection;

And the binding I can setup in XAML for the checkbox's isChecked property as you said.

Does this make any sense :)

Thanks for sticking around and helping me out.





Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

Neil Mosafi

Create a class to wrap your object and add a bool:

public class CheckedItem<TItem> : INotifyPropertyChanged
{
private TItem item;
private bool isChecked;

public CheckedVwAllServices(TItem item) { this.item = item; }

public T Item { get { return this.item; } }

public bool IsChecked {
get { return this.isChecked; } }
set
{
this.isChecked = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("IsChecked");
}
}


Then change you code to

VwAllServicesService AllServicesService = new VwAllServicesService();

VList<VwAllServices> AllServices = AllServicesService.GetPaged(FilterText.ToString(), "Service_Name ASC", 0, 30, out TotalCount);

ObservableCollection<CheckedItem<VwAllServices>> collection = new ObservableCollection<CheckedItem<VwAllServices>>();

foreach (VwAllServices item in AllServices)
collection.Add(item);

//Bind data to Itemscontrol

IC_Services.ItemsSource = collection;


You can then bind to the IsChecked property for your checkbox, and the Item.Center_ID and Item.Category_ID




Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

DSorre

Neil -

Thanks for the example. I am having a hard time understanding the Class to wrap the object and am getting the following error when I try to build:

Error 1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference )

And I think a couple parenthesis were wrong since VS gave me a couple errors, but I did get those sorted out.

Don't want to be a pain, but could you give me a quick run down of how it works I'm trying to figure out where to put my code to determine if the checkbox is checked or not and how to set it, my guess is put it in the for each loop, or not

Thanks again





Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

DSorre

Neil -

I got it to work using a totally different approach. In my view I just added an IsChecked field with a default of 0 and then I looped through my collection and depending on if the id is found in the cart I set the IsChecked value to 1 and bind my manually created collection to the checkbox.

You wouldn't believe how long it took me to get stupid checkboxes to be checked if the item is in the cart :) and I truly appreciate all the help from you it pointed me in the right direction or else I still would be trying to access the generated checkbox in the datatemplate.

Thanks again,

Dan





Re: Windows Presentation Foundation (WPF) How to determine whether a control sets an attached property programmatically

Neil Mosafi

No problems Dan, glad you sorted it out!

Regards
Neil