TemplateBinding in Xamarin.Froms not working - xaml

I tried this simple example from the official Xamarin page, that contains a template binding like:
<ControlTemplate x:Key="TealTemplate">
<Grid>
<Label Text="{TemplateBinding Parent.HeaderText}" />
<Label Text="{TemplateBinding Parent.FooterText}" />
</Grid>
</ControlTemplate>
However, i always get ths error by just copying the example code to Visual Studio.:
Can anyone give me a short example how to bind from within a ControlTemplate to a property of the templated control, be it of the binding context or the control itself?

I haven't worked out the issue with 'Parent'. But for my project I use:
{TemplateBinding BindingContext.IsBusy}
IN the page that use ControlTemplate, I also use a ViewModel and binding this ViewModel, which has IsBusy property, to the page.

Related

ContentPresenter Not Always Working in Silverlight Template

When we customize certain controls through a ControlTemplate, the ContentPresenter does not render the control as it should. Why is this?
It would really be easier if you let me see the affected xaml markup. My idea on what might be the problem is based on the rather dirty default behavior of the ContentPresenter: when it is used inside the ControlTemplate of a ContentControl, it will automagically bind to the Content and ContentTemplate properties. But it won't do this for any other control type. And therefore has to be done explicitly.
Do you have those bindings set:
<ControlTemplate ...>
...
<ContentPresenter Content="{TemplateBinding ...}"
ContentTemplate="{TemplateBinding ...}" />
...
</ControlTemplate>

XAML Binding to parent of data object Xamarin Forms

Xamarin Forms (XAML): How do I access the “parent” data context?
I found the 'nice' way to do this in Xamarin is to name the 'root' BindingContext on each page:
<ContentPage.BindingContext>
<AViewModel x:Name="RootContext" />
</ContentPage.BindingContext>
And then reference it in the element that is needed, without having to set an individual BindingContext. For example:
<Button Command="{Binding Source={x:Reference RootContext}, Path=StopCommand}"/>
A binding context is OTT sometimes, because everything below inherits it (sometimes without you knowing).
Have you tried the {x:Reference parent} markup?
I'm new at this myself, but if your element requires element to only one element (the parent in your case), you should be able to specify it.
In this example, you'd have to replace label by the parent element and from there the Binding would reference the right object.
<Slider x:Name="rotationXSlider"
BindingContext="{x:Reference label}"
Grid.Row="3" Grid.Column="1"
Maximum="360"
Value="{Binding RotationX, Mode=OneWayToSource}" />
Reference:
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_binding_basics/

binding tooltip to custom dependency property

this code work correct:
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls">
<TextBox x:Name="textBox"
ToolTip="{Binding Path=CustomToolTip,RelativeSource={RelativeSource AncestorType=local:TextBoxUserControl}}"/>
</UserControl>
but this code Does not work!!!
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls">
<TextBox x:Name="textBox">
<TextBox.ToolTip>
<ToolTip Text="{Binding Path=CustomToolTip,RelativeSource={RelativeSource AncestorType=local:TextBoxUserControl}}" Background="Yellow"/>
</TextBox.ToolTip>
</TextBox>
</UserControl>
i need create custom tooltip and bind it to CustomToolTip but in second code that's not bind to anything
where is the problem?
First of all, if we're talking WPF here, it should be <ToolTip Content="..."> instead of <ToolTip Text="...">, since ToolTip has no Text property.
Regarding the binding: Binding to other elements in the user control from within a ToolTip doesn't work since ToolTip elements are not part of the visual tree, as explained in another question that also provides one potential solution.
However, it seems that you're binding to some property defined in the UserControl's code-behind? In that case it's even easier to solve by setting the UserControl's DataContext to the control itself:
<UserControl x:Class="Extended.InputControls.TextBoxUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Extended.InputControls"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<TextBox x:Name="textBox">
<TextBox.ToolTip>
<ToolTip Content="{Binding CustomToolTip}" Background="Yellow"/>
</TextBox.ToolTip>
</TextBox>
</UserControl>
Alternatively, you could also set the DataContext in code-behind:
public TextBoxUserControl()
{
this.DataContext = this;
InitializeComponent();
}
In both cases, the CustomToolTip property can be accessed directly without the need for a RelativeSource binding.
An even better solution would be to introduce come kind of Viewmodel class that holds the CustomToolTip and all similar properties, and set this class as the UserControl's DataContext.

DataTemplate does not show result of bound children

On my MainWindow.xaml page, I have the following code which works (where MyWord is a string)
<ContentControl Content="{Binding MyWord}" />
I'm playing with DataTemplates and trying understand them. So, I want to reference a DataTemplate from within my ContentControl. The DataTemplate should contain a TextBlock which binds to my string. I updated my code
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" />
And in my ResourceDictionary I add
<DataTemplate x:Key="ViewsTemplate">
<TextBlock Text="{Binding MyWord}" />
</DataTemplate>
This produces no text on screen at all. I even tried
<ContentControl Content="{Binding MyWord}" ContentTemplate="{StaticResource ViewsTemplate}" />
and still no result on screen.
I can't work out why can any one give some advice please.
Thank you
The ContentControl still needs to have some content bound to it.
<ContentControl ContentTemplate="{StaticResource ViewsTemplate}" Content="{Binding MyWord}" />
Would work, but then you'd need to change your data template because it expects to be able to find MyWord which of course it won't be able to, so you'd want to use just {Binding} instead.
Alternatively, bind the ContentControl's Content to {Binding} - the current DataContext of its parent - and leave the template as it is.

Refresh custom control Xaml / lightswitch

I am creating an app in Lightswitch and I used this custom control.
The problem is when I change value of the control.
It's probably not possible to change control dynamically, it must be refreshed to get new value.
So I need to know how can I refresh just control or part of the screen.
This is how the control code look
<Grid x:Name="LayoutRoot" Background="White" Width="200" Height="200">
<GaugeControl:GaugeControl HorizontalAlignment="Left" Margin="10,10,10,10"
Name="gaugeControl1"
Maximum="25000" Minimum="0" VerticalAlignment="Top" />
</Grid>
This way is how I set binding to the value
public RadioGauge() {
InitializeComponent();
gaugeControl1.Value = Amount.amount; // where Amount is class and amount is int property
}
And this way I change the control property
SilverlightCustomControls.Amount.amount = 10000;
I couldnt find anything about refreshing or reloading XAML control or refreshing just part of lighswitch screen.. thx for any help
Lightswitch automatically refreshes components if they are data bound. Because you are just setting the value, the control is not being refreshed. Can you not bind the control to your property? Then when you change it in code the control should automatically refresh?
<Grid x:Name="LayoutRoot" Background="White" Width="200" Height="200">
<GaugeControl:GaugeControl HorizontalAlignment="Left" Margin="10,10,10,10"
Name="gaugeControl1"
Maximum="25000" Minimum="0"
VerticalAlignment="Top"
Value="{Binding Screen.SCREENDATASET.SelectedItem.Amount}" />
</Grid>