Silverlight Binding path to element - xaml

I'm having problem binding to correct object. How do I construct such path?
I'm binding Esri Silverlight control Graphic's layer to collection of TrackedAsset objects.
Now, inside graphics template I'm trying to bind to property of this TrackedAsset object.
It fails and this is what I see under locals. Seems like default binding inside of esri graphics points to ESRI.ArcGIS.Client.DataBinding object. This object has collection of "Attributes" and value of first of those attributes happen to be object I need.
I tried: {Binding Attributes.Values[0].IsGPSDataRecent}, no binding errors but I see no getter executed and it doesn't work as expected..

Related

In static method bindings that exist inside DataTemplates, is it possible to pass in the instance of the entity?

Example:
<DataTemplate x:DataType="FooEntity">
<Grid Background="{x:Bind MyClass.MyStaticBindingMethod(???)}" />
</DataTemplate>
It’s easy enough to pass in properties of the entity, but I can’t see a way to pass in the instance itself. Is this feature not supported? I could store a reference to this in FooEntity with a property called Instance (for example), and then go MyClass.MyStaticBindingMethod(Instance), but just want to make sure there isn't a cleaner way.
Relevant docs: https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension
You cannot pass the instance itself onto a DataTemplate.
The DataTemplate is only responsible for describing the visual structure of a data object and therefore its not part of its job to hold a explicit reference to any CLR object.
DataTemplate is defined to extend the visual structure of data objects, such as GridView/ListView, and those already handle the possibility of referencing to Collections, by setting the ItemsSource dependency property. Taking advantage of this, the DataTemplate simply exposes the customization of your visual structure. However this visual structure should be followed with the necessary bindings, to achieve your desired custom behavior.
If you are dealing with x:Bind, you will have to set the x:DataType, to the type of the Collection which you are setting as the Control ItemsSource, because the compiler needs that information at compile-time.
That is not a problem for Binding because those are evaluated at runtime, with the help of reflection.
I am not sure if I understand correctly, but to bind this, meaning the entity directly, you can just use empty binding:
Background="{x:Bind}"
or
Background="{Binding}"
This however means that FooEntity should derive from Brush so that it is compatible with Background type.

What is DataContext in XAML?

Please describe what is DataContext in XAML and when use it and relationship between {Binding} syntax. (with example) (In simple terms)
Thank you a lot.
Data contexts are described within the Microsoft documentation here.
A datacontext is data accessible to (aka within scope of) the current element. Thus a TextBox might have a datacontext set to SomeObject. If you where to bind to this using a binding, like this:
<TextBox DataContext={binding Path=SomeObject} Text={Binding Path=MyString} />
the textbox would have a datacontext containing the values in SomeObject. Any binding performed on this element (or any nested elements) will be within this scope. So, the binding on the 'text' dependency property would try to find the value in SomeObject.MyString.
It's important to note that the data context is passed down to its child elements: if a datacontext is set on a parent and you do not explicitly override this on its child elements, then the children will also have the same data context.

Changing a oneway bound TextBlock value from code behind.

I have a TextBlock and its Text property is bound to a ViewModel property. The binding is Oneway.
When I change the Text property of the Control from the xaml.cs the binding gets broken. If the binding is TwoWay I don't have this problem but the source property is updated too. Is it possible to have OneWay binding and change the target property value without braking the binding?
I suggest a workaround, like setting the Binding to TwoWay and ignore the update in the property. Something like this:
private string textValue;
public string TextValue
{
get { return textValue; }
set
{
:
}
}
Now the Property can no longer be set by the view.
Although no code is provided, this scenario typically occurs when you have bound a control to a view model and at a later stage your logic tries to update the value in the control programmatically.
You should not try to do this, that is define multiple sources of the value for a control. If you bind the control to a property on the view model, then to update the value in the control you should update the field in the view model.
If you were to set the value of a bound control programmatically at runtime so that it no longer matched the bound object value, when some other event causes the control binding to be re-evaluated, the value that you have provided programmatically would be overwritten again, you could easily end up with a scenario where the value you provided programmatically is never visible to the user.
In this scenario you should either:
Add a new property to the view model, bind this value to the control, then your program logic can set this value equal to the original property when the data is loaded, and updated when you need to
Not use bindings at all, always write to the control programatically that way you tightly control when the value is updated.
There is a workaround to this if you absolutely must have one. if you are using compiled bindings (x:Bind), then because the bindings are compiled it is possible to call SetValue on the bound dependency property at runtime and the previously compiled bindings will still be evaluated. However I advise against exploiting this because it makes your code a lot harder to follow and debug, when bindings are used, we tend not to look for code that directly addresses and sets control values.

Xaml Serialization

I want to use xaml serialization to store the configuration data of my application. This info is represented as readonly properties of a custom configuration class. When loading the application, the configuration class is deserialized through xamlservices api from a text file. However, I get an error in the form:
Cannot set unknown member 'property_name'.' Line number '24' and line position '4'.
This is because the setters are private. Is there a way to deserialize the object with readonly properties through xaml?
Yes, but you won't get round-trip serialization. You write a XAML document in which the object you want is created using a non-default constructor.
Look up info about the directive.
If its a collection property you can try using the ContentWrapperAttribute.
You can also look at the ValueSerializerAttribute
More info can be found here http://msdn.microsoft.com/en-us/library/ff354959.aspx

MVVM combobox binding issue

I have a project with a number of comboboxes, all binding correctly. Since installing SL5 I started experiencing all sorts of binding issues as well as the debugger being crippled. The problem is that when you switch from SL4 to SL5 and then back to SL4, not all the references are reset. Long story sort, I had to reinstall vs2010 to fix those issues. Now I am testing the dev environment and although I can get a textbox to bind from the VM, I cannot get a combobox to bind.
What I would like to know is what is the absolute minimum requirements for a combobox to bind. My ViewModel's property SET is getting its data.I have the comboboxes ItemSource set and the DisplayMemberPath set.I have tried using the ItemTemplate syntax but nothing works. The RaisePropertyChanged is being fired. Could this be a UI threading issue? The only difference between this combobox and the others is that there is a call to the database that collects values that are then used in a second call to the database. Those results are then passed to the property in the VM which raises the RaisePropertychanged event, but the binding doesnt render. I have mocked an entity in the class constructor and the binding works, but when I load the entities from the database it doesnt work.
I found the issue. This might be helpful to others. I was instantiating a ViewModels ObservableCollecion in the Completed event of the method and then looping through the collection from the database and addng it to the collection. Binding was not happening. I moved the instantiation of the ObservableCollection to the ViewModels constructor instead and the Binding started working. Still a bit puzzled as to why the newing up of the ObservableCollection has to be in the ViewModels constructor and not in the Completed event of the method fetching the data.