Windows Phone simple data binding issue - silverlight

Here is my XAML code:
<TextBlock x:Name="subTitle" Text="{Binding Name}" />
And my C# code:
PropertyInfo pi = ...;
subTitle.DataContext = pi;
The TextBlock displays simply nothing.
There is no DataContextChanged event but the TextBlock gets notified about the change because if I omit the path "Name" from the binding expression I get the ToString() representation of the DataContext. I can access no property of the bound object. There are no tools for debugging and I spent hours on this tiny but breaking issue.
Please help me solve this problem. Is it a bug or am I missing something? Thank you very much.

I'd guess that PropertyInfo.Name isn't a dependency property and/or it doesn't implement INotifyPropertyChanged.
Create your own viewmodel object (which implements INotifyPropertyChanged) and use that for binding values to the view.

Related

How to bind a textblock's text through XAML using a property?

I am working on a Silverlight application, and I want to bind the simple text property of textblock through a property of string type.
What I did was:
<TextBlock Text="{Binding Name}"/>
Code behind:
public string Name{get;set;}
Name = "Testing..!";
but it will not work.
To expand on anatoliiG's answer (which will work): Data binding refers to properties on the DataContext property of the current element by default. This means that your
<TextBlock Text="{Binding Name}" />
is actually translated to
Set the value of the Text property to this.DataContext.Name
(DataContext is inherited, so if it is not explicitly set on the TextBlock it will check the parent, then the parent of the parent etc etc)
You can resolve your problem in one of two ways:
You can set the value of this.DataContext on the parent to the parent itself (as anatoliiG suggests). This means that when it looks up this.DataContext.Name it will be checking the Page itself, which is where your Name property is found.
You can change your Binding so it looks at the Page instead of Page.DataContext when it is looking up bindings. You can achieve this using the RelativeSource markup extension:
This translates to:
Find the first ancestor of the TextBlock that is of type Page, and bind to the Name property on that object
As a final note, you will also need to implement INotifyPropertyChanged on your DataContext object if you are going to ever change the value of Name.
Oh, and you should be using view models as the DataContext instead of the Page itself!
Answer to your question is: in Page_Loaded event set LayoutRoot.DataContext = this;. But it is more hack, than good practice.
You should take a look into MVVM pattern and INotifyPropertyChanged and create ViewModel which will contain this property.

Visifire/WPF databinding issues (Visifire v3.6.8)

I have a very basic databinding issue with the Visifire WPF charting tool in the last open-source version, v3.6.8, before it became commercial. I want to bind an ObservableCollection<> to the DataSource-Property of a DataSeries. It just doesn't work when I bind the property in XAML. When I bind the data in the code behind, it works fine.
I am using the collection for a DataGrid as well and there everything works fine.
The output log doesn't show any binding issues.
The get/set accessors of the DataSource property of the DataSeries object are never accessed when binding the property in XAML, I just don't get why. The dependency property is registered at creation of the DataSeries object.
Are there any known issues with this or am I missing something basic?
The XAML:
<DataGrid x:Name="grid" Grid.Column="0" ItemsSource="{Binding TransverseParallelShearLCS}"></DataGrid>
<Charts:Chart Grid.Column="1" x:Name="chart" ZoomingEnabled="True" AnimatedUpdate="True" ScrollingEnabled="False">
<Charts:Chart.Series>
<Charts:DataSeries RenderAs="Line" DataSource="{Binding TransverseParallelShearLCS}" AutoFitToPlotArea="True">
<Charts:DataSeries.DataMappings>
<Charts:DataMapping MemberName="XValue" Path="X"></Charts:DataMapping>
<Charts:DataMapping MemberName="YValue" Path="Y"></Charts:DataMapping>
</Charts:DataSeries.DataMappings>
</Charts:DataSeries>
</Charts:Chart.Series>
</Charts:Chart>
Please try the below sample example from Visifire Example Area and check.
http://visifire.com/silverlight_examples_details.php?id=10

Get reference to Xaml object in view model

I have a an object created in Xaml:
<Grid>
<MyObject/>
</Grid>
I need someway to bind the object myObject back to a property in my view model. I dont know whether this is possible, everything ive seen so far binds properties together, but any help would be greatly appreciated.
I am assuming what you want is your ViewModel to hold the actual visual control MyObject in it and your Grid to display it via MVVM.
This is possible through ContentControl in WPF.
Assuming your ViewModel has a property MyObjectView which holds MyObject...
<Grid>
<ContentControl Content="{Binding MyObjectView}" />
</Grid>
Having said that you must take caution that same MyObjectView is not bound to any other content control as that will result in an error
"Specified element is already the logical child of another element.
Disconnect it first"
And if that requirement is possible then you must excercise ContentTemplate option.
Let me know if this helps.
It is possible. It kinda breaks mvvm though.
You can attach an InvokeCommandAction to this object, and bind the CommandParameter to it via ElementBinding. Then in the callback of the command which you defined in the viewmodel, you will have a reference to this object from the CommandParameter.

WPF Update Binding when Bound directly to DataContext w/ Converter

Normally when you want a databound control to 'update,' you use the "PropertyChanged" event to signal to the interface that the data has changed behind the scenes.
For instance, you could have a textblock that is bound to the datacontext with a property "DisplayText"
<TextBlock Text="{Binding Path=DisplayText}"/>
From here, if the DataContext raises the PropertyChanged event with PropertyName "DisplayText," then this textblock's text should update (assuming you didn't change the Mode of the binding).
However, I have a more complicated binding that uses many properties off of the datacontext to determine the final look and feel of the control. To accomplish this, I bind directly to the datacontext and use a converter. In this case I am working with an image source.
<Image Source="{Binding Converter={StaticResource ImageConverter}}"/>
As you can see, I use a {Binding} with no path to bind directly to the datacontext, and I use an ImageConverter to select the image I'm looking for. But now I have no way (that I know of) to tell that binding to update. I tried raising the propertychanged event with "." as the propertyname, which did not work.
Is this possible? Do I have to wrap up the converting logic into a property that the binding can attach to, or is there a way to tell the binding to refresh (without explicitly refreshing the binding)?
Any help would be greatly appreciated.
Thanks!
-Adam
The workaround here was to add a property to my object (to be used as the datacontext) called "Self" , which simply returned
public Object Self { get { return this; }}
Then in the binding I used this property:
<Image Source="{Binding Path=Self, Converter={StaticResource ImageConverter}}"/>
Then when I call
PropertyChanged(this, new PropertyChangedEventArgs("Self"))
it works like a charm.
Thanks all.
I don't believe there is a way of accomplishing exactly what you need with your current converter. As you mentioned, you could do the calculation in your ViewModel, or you could change your converter into an IMulitValueConverter.
From your specific scenario (the converter tied to a ViewModel class, and a few of its properties), I would lean towards implementing the logic in the ViewModel.
Hmm, you don't show the full implementation. But I think it should update, if the value bound to the GUI provides the PropertyChanged-Event.
Regards

Is there a way to convert WPF binding markup to and from an instance of the binding class?

For example I have the following binding markup
Text="{Binding Path=FirstName}"
pretty simply but it could be much more complex, I need to be able to parse this markup and get it into some objectified form such as an instance of the Binding class.
Something that could work in reverse, an instance of the binding class to spit out the markup would be great as well.
I know such a thing must exist in the framework but I dont know where/what class.
I have looked at XamlReader but was unable to get it working because in this case I am missing context as I am only working with bits of the project and not the whole.
You can get the Binding object using GetBindingExpression, for example if you have:
<TextBlock Name="MyTextBlock" Text="{Binding Name}"/>
You can use:
BindingExpression expr = BindingExpression.GetBindingExpression(MyTextBlock, TextBlock.TextProperty);
Binding bindingObject = expr.ParentBinding;
To use XAMLReader you have to surround it with a valid root. then this shoudl work.

Resources