XAML Binding to parent of data object Xamarin Forms - xaml

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/

Related

use different data types in xaml properties

<Picker
Title="Select a Profile"
ItemsSource="{Binding ProfileManager.Profiles}"
ItemDisplayBinding="{Binding Name}"
SelectedItem="{Binding SelectedProfile, Mode=TwoWay}"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand"
SelectedIndex="1"
HeightRequest="200"
WidthRequest="325"
Margin="10"
BackgroundColor="{StaticResource Secondary}">
</Picker>
the itemSource property have x:DataType="ViewModels:ParametersViewModel" datatype but the ItemDisplayBinding have x:DataType="Models:Profile" datatype.
How can I define these datatypes seperatly?
Not sure If this answer works for you but you can define different data types inside same XAML file by clarifying Ancestor Type.
<ContentPage
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Models="clr-namespace:Models"
xmlns:ViewModels="clr-namespace:ViewModels"
x:DataType="viewmodel:ParametersViewModel">
<Picker ItemsSource="{Binding ProfileManager.Profiles}"
ItemDisplayBinding="{Binding Source={RelativeSource AncestorType={x:Type Models:Profile}}, Path=Name}"/>
</ContentPage>
Once you set a DataType in the root (in this case ContentPage), all the bindings of its children will go to that datatype by default.
From document Populate a Picker with data using data binding, we could find that
A Picker can be also populated with data by using data binding to bind
its ItemsSource property to an IList collection.
<Picker Title="Select a monkey"
ItemsSource="{Binding Monkeys}"
ItemDisplayBinding="{Binding Name}" />
When binding to a list of objects, the Picker must be told which
property to display from each object. This is achieved by setting the
ItemDisplayBinding property to the required property from each object.
In the code examples above, the Picker is set to display each
Monkey.Name property value.
So, the ItemDisplayBinding property(Name) in your code above should be the required property from each object(Profile) in your ItemsSource(ProfileManager.Profiles).
In fact, we don't have to or need to specify what you call x:DataType for the Picker.
Note:
You can find the official sample here MonkeyAppPicker,though it is coded in xamarin , but the implementation on MAUI is almost the same.

TemplateBinding in Xamarin.Froms not working

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.

How to change the width of a control dynamically in UWP?

Tabbar is a very common navigation control on iOS and Android. But UWP doesn't seem to have.
I've seen an example XamlPivot(SHORTCUT)①, Use Pivot to make TabBar, The effect is very good, and I tried to modify it, so that the TabBar in the bottom, content in the upper.
My project is MasterDetail, Master part is TabBar(i.e TabsStyle Pivot), Detail part just a blank.
Now I found a big problem, TabBar each item does not automatically divide the width, then I try to use data binding and value converters to dynamically provide the width, the binding source is the ActuallyWidth of the MasterGrid, but the ActuallyWidth does not change with the window size, and when the Window on WideState, the Mater part will become blank.
So, How to change the width of the TabBarItem dynamically?
Various window size effect chart(Remove"()"):
(https:)//i.stack.imgur.com/3GE5t.png
(https:)//i.stack.imgur.com/FyQuX.png
(https:)//i.stack.imgur.com/pChwz.png
(https:)//i.stack.imgur.com/cib1l.png
XAML:
<Pivot x:Name="pivot"
Style="{StaticResource TabsStylePivotStyle}">
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 1" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 2" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
<PivotItem>
<PivotItem.Header>
<local:TabHeader Width="{Binding ActualWidth, Converter={StaticResource AutoWidthConverter}, ElementName=pivot, Mode=OneWay}"
Glyph=""
Label="item 3" />
</PivotItem.Header>
<TextBlock Text="Content content content" />
</PivotItem>
</Pivot>
Converter:
public object Convert(object value, Type targetType, object parameter, string language)
{
return (double)value / 3;
}
For the record, ActualWidth is NOT a DependencyProperty in UWP XAML model - so can't participate in bindings properly (it doesn't notify of changes).
So if you wish to do a binding like you're doing, you're going to need to expose ActualWidth in a bindable way. One of the easier ways of doing so is to a create a Behaviour explicitly for that, that attaches to the SizeChange event of the target element (in your case the pivot), and returns it's ActualWidth / ActualHeight / RenderSize as DependencyProperties on the behaviour. Your TabItems would then look to the ActualWidth on the behaviour instead.
(It's not done by default presumably because UWP XAML doesn't have read-only dependency property support, and binding too it can easily lead to circular layout cycles when layout rounding is in play if you're not careful)

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>