Access parent binding context from dataTemplate resource (x:Reference) - data-binding

I got a page in which I load multiple control template considering some business logic binded to a property in my viewModel.
<ContentView x:Name="ContentView" ControlTemplate="{Binding ItemControlTemplate}">
</ContentView>
Inside this control template I got a CarouselViewControl which uses a DataTemplate.
<carousel:CarouselViewControl x:Name="CarouselViewMapTemplate" Position="
{TemplateBinding BindingContext.CarouselPosition}"
PositionSelectedCommand="{TemplateBinding BindingContext.PositionChanged}"
Orientation="Horizontal" AnimateTransition="True" IsSwipeEnabled="False"
ItemsSource="{TemplateBinding BindingContext.Items}" InterPageSpacing="10"
ItemTemplate="{StaticResource ItemsDataTemplate }">
</carousel:CarouselViewControl>
And inside this DataTemplate I would like to bind to a property in the ViewModel, which is also a DataTemplate.
<maps:SfMaps x:Name="sfmap" EnablePanning="False" EnableZooming="False" BackgroundColor="Transparent">
<maps:SfMaps.Layers>
<maps:ShapeFileLayer x:Name="ShapeLayer" Uri="FRA_adm0.shp" MarkerTemplate="{Binding Parent.BindingContext.MarkerDataTemplate}">
</maps:ShapeFileLayer>
</maps:SfMaps.Layers>
</maps:SfMaps>
How can I achieve that ? I just tried
{Binding Parent.BindingContext.MyProperty}
but this is not working. I also digged with Source property of binding, but that won't do it as I don't have reference in my DataTemplate, used as resource in the app level ResourceDictionary.
This DOESNT work ofc it does not find carouselView in the ResourceDictionary :
"{Binding Path=BindingContext.DisplayMarkerTemplateCommand, Source={x:Reference carouselView}}"
Not being able to do that breaks the whole point of having ResourceDictionary and clean templating if binding is not working from it with XAML.
THANKS !

Related

Binding to the SelectedItem in a ListBox which is in a DataTemplate for a ContentControl

Using an MVVM approach, I have a View that contains a ListBox and also a Grid in which I want to display information about the SelectedItem in the ListBox. I want to set the DataContext for the Grid to the SelectedItem.
However, the ListBox is buried as follows: A ContentControl that is bound to a DataTemplate that is a UserControl View that contains the ListBox.
Here is the MainWindow Grid that I'm not sure how to bind:
<Grid DataContext="{Binding ElementName=MyList, ????}">
Here is the ContentControl in the same View:
<ContentControl x:Name="MyList"
Content="{Binding}"
ContentTemplate="{StaticResource MyListTemplate}"/>
Here is the Data Template in the same View:
<Window.Resources>
<DataTemplate x:Key="MyListTemplate">
<v:MyListView/>
</DataTemplate>
</Window.Resources>
Here is MyListView:
<UserControl>
<ListBox Name="MyListBox" ItemsSource="{Binding ItemList}"/>
</UserControl>
I am adding to code I wrote a couple of years ago and have been away from WPF for a while, so alas, I am rusty on my data binding. I have been trying to add a SelectedItem property to the view model for MyListView and/or the MainWindow. I expect this may require RelativeSource.
Doh! I was forgetting to specify the OnPropertyChanged call for my property.
In the UserControl ListBox, I needed this:
ItemsSource="{Binding ItemList}" SelectedItem="{Binding MySelectedItem}"
In the main window view model, I needed this:
public MyItemViewModel MySelectedItem
{
get { return _mySelectedItem; }
set
{
_mySelectedItem = value;
OnPropertyChanged("MySelectedItem");
}
}
Then, in the main window, the binding is simply:
<Grid DataContext="{Binding MySelectedItem}">

Xceed Datagrid won't bind to DataTemplate of Xceed control

I have the following DataTemplates defined. The TextBlock works the xctk:ShortUpDown does not. In fact whenever I use a control from another namespace it doesn't work (i.e. no data displayed or updated
<DataTemplate x:Key="intDataTemplate">
<TextBlock Text="{Binding StringFormat=\{0:F0\}}"/>
</DataTemplate>
<DataTemplate x:Key="hexDataTemplate">
<xctk:ShortUpDown ParsingNumberStyle="HexNumber"/>
</DataTemplate>
These are the column definitions. There is no CellEditorTemplate available.
<xcdg:Column FieldName="Coefficient" Width="75"
CellContentTemplate="{StaticResource hexDataTemplate}" ReadOnly="False"/>
<xcdg:Column FieldName="Measured" Width="75" CellHorizontalContentAlignment="Right"
CellContentTemplate="{StaticResource intDataTemplate}" />
There just doesn't seem to be a lot of example code out there. The columns are auto generated.
Any suggestions are appreciated.
The CellContentTemplate is for display purposes only. If you put a control meant for editing in it, such as a ShortUpDown, you will get weird results.
Editor controls should be defined in the CellEditor. Also, don't forget to set the CellEditorBinding to connect it to the underlying value.
<xcdg:CellEditor x:Key="hexCellEditor">
<xcdg:CellEditor.EditTemplate>
<DataTemplate>
<xctk:ShortUpDown Value="{xcdg:CellEditorBinding}" ParsingNumberStyle="HexNumber"/>
</DataTemplate>
</xcdg:CellEditor.EditTemplate>
</xcdg:CellEditor>
<xcdg:Column FieldName="Measured" CellEditor="{StaticResource hexCellEditor}" ... />

WPF Datagrid binding and column display, not able to display the Data in the DataGrid after binding the DataGridTextColumn Header

Initially I was generating a dataGrid with AutoGeneratedColumns = True getting values from a dataTable. However the number of columns for each dataset is fixed and I would like to name them accordingly and therefore named them in the code but if the Tab was not selscted when I runed the code then I was getting an error that the Index is out of range.
I decided to do AutoGeneratedColumns = False and manually define my columns. However in the sequel my data dataGrid was empty and my data not displayed. The Data Table name is Underlyings and it is generated at runtime, before that its not even there.
Below my XAML code:
<DataGrid ItemsSource ="{Binding Path=Underlying}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="462,145,0,203" Name="dataGrid3" Width="872" >
<DataGrid.Columns>
<DataGridTextColumn Header="In" Binding="{Binding FirstColumnName}" />
<DataGridTextColumn Header="InL" Binding="{Binding SecondColumnName}" />
<DataGridTextColumn Header="Total " Binding="{Binding ThirdColumnName}" />
</DataGrid.Columns>
</DataGrid>
And in the code thwe following
dataGrid3.ItemsSource = Underlying.DefaultView
However I think that XAML binding would not work for me because my datatable is generated at runtime so there is nothing to bind to at design time.
Any Ideas how to do so?
I think you should not set the ItemsSource property but rather use the DataContext property of the DataGrid and use templating in the XAML as in the question here: WPF: Binding to DataGrid

Windows Phone app: Binding data in a UserControl which is contained in a ListBox

I have an "AddressListBox" ListBox that contains "AddressDetails" UserControl items, as shown in the .xaml file extract below. The Addresses collection is defined as
ObservableCollection< Address > Addresses
and Street, Number, PostCode, City are properties of the Address class. The binding fails, when I use the "{Binding property}" syntax shown below. The binding succeeds, when I use the "dummy" strings in the commented-out code. I have also tried "{Binding Path=property}" syntax without success. Can you suggest what syntax I should use for binding the data in the user controls?
<ListBox x:Name="AddressListBox"
DataContext="{StaticResource dataSettings}" ItemsSource="{Binding Path=Addresses, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<!--
<usercontrols:AddressDetails AddressRoad="dummy" AddressNumber="dummy2" AddressPostCode="dummy3" AddressCity="dummy4">
</usercontrols:AddressDetails>
-->
<usercontrols:AddressDetails AddressRoad="{Binding Street}" AddressNumber="{Binding Number}" AddressPostCode="{Binding PostCode}" AddressCity="{Binding City}">
</usercontrols:AddressDetails>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You need to set the datacontext on the usercontrol to the item like this.
<usercontrols:AddressDetails DataContext={Binding}
...
</usercontrols:AddressDetails>
Now your other bindings should work correctly as they are relative to the item.

SL4 Binding problem with WCF RIA Services

Basic problem: How do I bind a textbox to the selected item of a combobox who's itemsource is the result of a LINQ query on a WCF RIA Services Domain context.
Additional requirement: When the selected item is changed the binding should update.
Additional requirement: The binding should be two way.
My solution so far:
After the user clicks to create a new item, new items are created and added to the object set but not persisted yet (one for each language). After that this code runs. The combobox is supposed to allow the user to select one of the created items, which is displayed as it's corresponding language. The bound textboxes should allow the user to edit the item.
Code behind:
//Setup the combobox
LanguageComboBox.ItemsSource = dc.GeneralStatistics.Where(g => g.RelatedResourceId.Equals(guid));
LanguageComboBox.DisplayMemberPath = "Language.LanguageName";
LanguageComboBox.SelectedItem = dc.GeneralStatistics.First(g => g.Language.LanguageName.Equals("English"));
//Setup the textboxes
this.StatisticsText.DataContext = (LanguageComboBox.SelectedItem as GeneralStatistics).Text;
this.ShortDescriptionText.DataContext = (LanguageComboBox.SelectedItem as GeneralStatistics).ShortDescription;
XAML CODE:
<ComboBox x:Name="LanguageComboBox" />
<TextBox x:Name="ShortDescriptionText" Text="{Binding}" />
<TextBox x:Name="StatisticsText" Text="{Binding}" />
The problem with my solution:
It does not work, because when I change the selection in the combobox the textboxes do not update. I could implement the selection changed event handler and manually update the textboxes, but that would defeat the purpose of binding the textboxes. What is the best practice here?
You can simplify the code by doing the following.
Code behind:
LanguageComboBox.DataContext = dc.GeneralStatistics.Where(g => g.RelatedResourceId.Equals(guid));
XAML:
<ComboBox x:Name="LanguageComboBox" />
<TextBox x:Name="ShortDescriptionText" Text="{Binding ElementName=LanguageComboBox, Path=SelectedItem.ShortDescription}" />
<TextBox x:Name="StatisticsText" Text="{Binding ElementName=LanguageComboBox, Path=SelectedItem.LongDescription}" />

Resources