Is it possible to bind to a property name? - c#

Is it possible to bind a property name. I seem to come up with run time errors when I try.
For instance:
<button Name="{Binding UniqueID}" Click="ButtonHandler">
This being in a header for a collection in a grid-view...

You can't bind Name, sorry. It's used for too many things internally and stuff would surely go crazy if you could. The docs are a bit vague, but do say this: (emphasis mine)
You cannot use the string value of Name as a
direct source value for a data binding source. If you have to display
the same string value as Name in UI with binding, you should replicate
the same value to the Tag property, which can be used as a property
binding source. Also don't use Name as a binding target.
(MSDN: FrameworkElement.Name)
However, if you want to attach random extra data to UI controls, I would recommend using attached properties instead. That way they're specifically associated with what you're doing and will be appropriately typed, unlike Tag.
(MSDN: Custom Attached Properties)

Well, I've read the documentation over and over and cannot find a way to make it work. The documentation doesn't say you cannot do it, but it doesn't say you can do it either.
However, I found two workarounds. Instead of binding the name, if you aren't using Tag or DataContect, you can find to those and in the handler extract them by casting as a string.
It's not elegant, but, it does seem to work as expected.

Related

Binding to a property by variable name (ideally from xaml)

I'm trying to databind (ideally from XAML as i know how to do this in code behind but it would be far from trivial to traverse my heavily templated tree just for that) to a property who's name i only know at runtime
What i would like to do is not the usual:
Content="{Binding TheProperty}"
But something like
Content="{Binding PropertyName=TheNameIsStoredInThisProperty}"
I'm trying to do this because i generate the UI from templates when binding to my plugins, but the UI is specified in a set of POCO and separate from the ViewModel, so i want to be able to generate my UI and still be able to wire it to the correct properties on the ViewModel, any advice is most welcome.
The immediate solution might be to bind to an arbitrary property in your VM with an IValueConverter that goes both ways, and the ConverterParameter is the string containing the source property name. Once inside the value converter you can use an interception pattern to Reflect out the value you need from the POCO. You can then pass the value up to the source property in the VM. Rather like a pipeline :) This will work but still leaves you with being notified when the POCO changes.
A Markup Extension seems plausible but likely to be brittle and provide naught in the way of performance improvement.
An Attached Behaviour still leaves you with having to Reflect and does not easily solve the problem of notifications originating in the POCO (AFAIK only Unity knows how to do that).

How can I retrieve name(Key) of applied template?

I have a template assigned to a button:
<Button x:Name="btn_PatMat" Template="{StaticResource PatMat_Button}" ...
How can I retrieve the Key/String/Name of this template from said button?
Pseudocode:
String = btn_PatMat.Template.???.ToString()
You can't. At least not in the way you're trying.
To quote from this SO post about x:Key (emphasis mine):
x:Key is used for items that are being added as values to a
dictionary, most often for styles and other resources that are being
added to a ResourceDictionary. When setting the x:Key attribute,
there is actually no corresponding property on the object or even an
attached dependency property being set. It is simply used by the
XAML processor to know what key to use when calling Dictionary.Add.
StaticResources are evaluated during loading, so once the control loads, the Template property is no longer set to a binding, but is instead set to a copy of the ControlTemplate from your Resources, and no corresponding property on that object is set to the key.
You can verify this by checking out the XAML of the button after it's loaded, by using something like XamlWriter.Save to view it's XAML string.
The only solution I can think of that might work would be to loop through your .Resources, and find a ControlTemplate that is equal to your Button's ControlTemplate. I haven't tested this, and it probably isn't very practical for large resource libraries, but it may be an option.
But a better solution would probably be to change your logic so the key value can be accessed some other way by whatever object needs it.
Well I'm afraid that's not possible because it's not intended by WPF. There are some people which wanted to get access to x:Name which is similar to x:Key, they all had to give up.
Pls have a look at this SO post and this additional link.
The only workaround I could imagine is reading all templates from the ResourceDictionary, instantiate each resource (if possible), find the template (if it's e.g. a style) and compare it with the current instance of the template found in the control. But this seems to be a pretty ugly solution and I'm not sure if it'll work without any problems.

Why ever use a multi binding converter?

Couldn't I just use a single binding converter and as a parameter pass in the DataContext and from there pick what properties I want to use?
If you pass the whole object instead of the individual properties, then the binding expression will not be re-evaluated when the individual properties change. You will be losing the benefit of the INotifyPropertyChanged mechanism.
You might want to be more explicit and take in the minimum extra information (which is just generally good programming practice), or you may want information from more than one source - e.g. Your value might be dependent on a property of the datacontext and the checked state of a checkbox somewhere else in the view.
You can do that, but the binding will not update if the relevant properties change that way. Besides the updates Multibinding is needed for more complex bindings to different controls and data-objects.

WPF check if a dataproperty has an binding

Hej,
I'm trying to make som general functionality for my ListView, so that the content of a ListView can be exported to CSV directly.
I'm trying to achive this by getting the datacontext and analysing the ICollectionView for this. From here I have access to the all the objects from via ICollectionView via SourceCollection, in which I (for now) presume sorting/and filtering is respected.
The challange here is that I only want to output the columns that also are showed in the ListView.
When iterating my collection, is there a function where I can evluate if a property in a class (with notification suppoert) has a binding to it?
The esiest solution for now would be just to output all properties, but I'm not interested in this, since oid's are not fun to look at.
Thx in advanced.
/Ian
I suggest you recognize that the ability to determine which data is being displayed is a business requirement. Thus, you should embody this requirement in your model. In other words, your model should clearly indicate which columns are visible - you shouldn't be trying to infer this from your existing properties, nor should you be examining your view.
There are a whole bunch of ways you could do this, but the key is to have this information on hand in your model.
Why don't you just look at the DataTemplate and evaluate it including the binding inside?
There's no easy way to do it... You can check the binding on the target side (dependency property), but not on the source side.
For what you're trying to do, you could loop through the columns of the ListView and check their DisplayMemberBinding, but it could be undefined (the cell content might be defined using the CellTemplate property instead).

What is the use of Tag property in Tree view control C#?

What is the use of Tag property in Tree view control C#?
How can we work with it?
A common use for the Tag property is to store data that is closely associated with the control (from MSDN). Any type derived from the Object class can be assigned to this property.
It's a cheap way of avoiding inheritance to add just one Property.
Every control that inherits from Control in winform has a Tag property where you can store metadata for later use, for example you can store database id in that property for every item and load data from database on tree node click
As ArsenMkrt said, every control that inherits from Windows.Forms.Control has the Tag property. This is of type System.Object, so you can store anything you want.
The idea of the Tag property probably comes from VB6, which also has this, but in VB6 it is limited to String values.
When writing a UI, sooner or later you will find yourself handing an event in which you know the UI control that the event came from, but you also need to know what backing data that control is associated with. Usually, that problem can be solved with data binding, but not always. In the latter case, you can manually populate the Tag property with whatever you need to make the code work.

Categories