In WPF, how do I bind in the DataGrid.RowDetailsTemplate? - datagrid

I have a DataGrid which is bound to an ObservableCollection of a custom class. The collection is a property in a ViewModel, and the grid works fine, including with changes to the collection.
I want to use the RowDetailsTemplate to display two additional collections in a StackPannel, like so:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Name="GrcidSP" Orientation="Horizontal">
<DataGrid Name="ClusterIndexGrid" AutoGenerateColumns="True"
ItemsSource="{Binding ClusterIndexColumns}"
CanUserAddRows="False" CanUserResizeRows="False"
Width="Auto"/>
<DataGrid Name="IndexGrid" AutoGenerateColumns="True"
ItemsSource="{Binding IndexColumns}"
CanUserAddRows="False" CanUserResizeRows="False"
Width="Auto"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
ClusterIndexColumns and IndexColumns, the binding sources of the inner grids, are also collections in the ViewModel.
Problem is, when i display the rowdetails, the datagrids are empty, i.e. not loaded at all, no columns or rows.
In a bid to understand what is going on, i replaced the inner datagrids with label:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Name="GridSP" Orientation="Horizontal">
<Label Content="{Binding}" Width="Auto" Background="AliceBlue"/>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
and did not specify the binding source - {Binding}
The label now displays the name of the custom class which is in the collection that is the source of the outer grid.
So somehow, i need to navigate back to the ViewModel as the data context for the inner grids.
But how, pray tell?

OK, so I solved this by adding the ClusterIndexColumns and IndexColumns collections as properties to the custom class which is the displayed in the main grid. Now, when i click on the row, the rowdetails area is opened and the inner grids are loaded fine.
BTW, the collections are populated when the selected row changes in the main grid, by binding the SelectedIndex property to a property in the ViewModel, using
Mode=OneWayToSource
Hope this helps someone.

Related

Xamarin.Forms CollectionView trigger event on Focus/Hover

I'm currently attempting to implement a Xamarin.Forms application which will be targeting Android TV. I have simplified my application to show the specific issue I'm having.
I have a collection of items which which I am displaying via a CollectionView. I am able to scroll around items inside this CollectionView using the D-Pad controls absolutely fine, as can be seen via this screenshot, where the "focussed"/"selected" item is visible if I make the layout bounds visible.
There is nothing available except the "ItemSelected" event on the CollectionView which only occurs on actually pressing enter. I have tried to add in "Focus" events to all the elements that make up this page and nothing gets triggered. I am unsure what exactly is being "focussed" upon here.
This is my page:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:Channels.ViewModels"
x:Class="Channels.MainPage"
x:DataType="viewmodels:ChannelViewModel"
BackgroundColor="{DynamicResource BackgroundColour}">
<StackLayout Orientation="Vertical" Padding="10">
<Label Text="Selected Item" TextColor="White" Margin="0, 0, 10, 10"></Label>
<CollectionView ItemsSource="{Binding Data}">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="5"></LinearItemsLayout>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Padding="5" Spacing="5" BackgroundColor="Red">
<Label Text="Test Text" HorizontalTextAlignment="Center" HorizontalOptions="FillAndExpand" TextColor="White"></Label>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage>
I would like to know exactly what is getting focus here and if it is possible to do something like I have stated.
I haven't found many examples of Xamarin being used in terms of Android TV so it might not be possible to do things like this. I have also tried implementing the same code in .NET Maui and cannot find any events that might work here.
You could use the GestureRecognizers in the template to triger what you want to do when you selected the item or click on the item.
<Label>
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Label.GestureRecognizers>
</Label>

CollectionView issue mixing grouping with GridItemsLayout

I'm trying to implement a Xamarin Forms CollectionView using Grouping and GridItemsLayout vertical mode and Span="2". I make the grouping work without problems, but when I add the GridItemsLayout with the Span set to 2 columns, it does not behave as it should. Still showing the collection as 1 column.
<CollectionView
Grid.Row="1"
IsVisible="{Binding VisibleCollection}"
IsGrouped="True"
ItemsSource="{Binding Accounts}">
<CollectionView.GroupHeaderTemplate>
<DataTemplate>
<Label
FontSize="16"
Text="{Binding Name}" />
</DataTemplate>
</CollectionView.GroupHeaderTemplate>
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
Span="2"
VerticalItemSpacing="10" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Label BackgroundColor="Red" Text="TEST" />
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
Label "TEST" still showing in 1 single column, and I want it to be shown as 2 columns:
TEST TEST
instead of:
TEST
TEST
Anyone knows how I can make it possible with GridItemsLayout? btw, this issue is happenning on UWP
Label "TEST" still showing in 1 single column, and I want it to be shown as 2 columns
Thanks for taking time to report this problem, for my testing the problem occurs in Xamarin Forms earlier version, and it is fixed in current stable version. Please update your Xamarin.Forms Nuget Package version to 4.5.0.530 . And please note CollectionView is available on iOS and Android, but is only partially available on the Universal Windows Platform. For more info please refer this document.
The problem comes when the collection view is initially "hide" and switching visibility.
Xamarin forms issue: https://github.com/xamarin/Xamarin.Forms/issues/9079
Try to set GridItemsLayout from main thread.
Example
Device.BeginInvokeOnMainThread(() =>
{
BannerList.ItemsLayout = new GridItemsLayout(2,ItemsLayoutOrientation.Vertical);
BannerList.ItemsSource = ImageList;
});
This is worked for me.

Specify design time binding context in a GridTemplateColumn

I'm working on a Xamarin.Forms project. In my xaml page, order to avoid "Cannot resolve property 'xxx'" warning, I've set a design-time data context.
In this page I have a SfDataGrid object.
In the following code snippet, the text property of the custom entry is binding to the Quantity field of a Part item. The Parts object is an ObservableCollection<Part>.
<xForms:SfDataGrid ItemsSource="{Binding Parts}" AutoGenerateColumns="false" Margin="0"
ScrollingMode="PixelLine"
HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" >
<xForms:SfDataGrid.Columns>
<xForms:GridTextColumn MappingName="ProductName" HeaderText="Product"
HeaderTextAlignment="Start" ColumnSizer="Star"
TextAlignment="Start" Padding="5,0,5,0"/>
<!--#region Quantity column -->
<xForms:GridTemplateColumn MappingName="Quantity" HeaderText="Quantity"
HeaderTextAlignment="Center"
ColumnSizer="Auto" Padding="0" >
<xForms:GridTemplateColumn.CellTemplate>
<DataTemplate>
<controls:CustomEntry Text="{Binding Quantity, Mode=TwoWay,
Converter={StaticResource NullableDouble}}"
Keyboard="Numeric" HorizontalTextAlignment="Center"
VerticalOptions="End"/>
</DataTemplate>
</xForms:GridTemplateColumn.CellTemplate>
</xForms:GridTemplateColumn>
<!--#endregion -->
</xForms:SfDataGrid.Columns>
</xForms:SfDataGrid>
The warning is displayed under the Quantity word in Text={Binding Quantity, Mode...
What should I do to avoid the warning for the binding used in the cell templates?
Should I add a BindingContext="{d:DesignInstance models:Part}", or d:DataContext="{d:DesignInstance models:Part}" on each field ?
Hi Crusty Applesniffer,
Thanks for using Syncfusion Product.
We have checked your query with following details.
By using SfDataGrid have created the GridTemplateClolumn and added CustomEntry as DataTemplate in XAML page
Text property of this custom entry is binding to the double type
property named as OrderId
We have set this Text property binding
Mode as TwoWay, And we have written the converter for this property.
And we have added the Xaml
Complilation([XamlCompilation(XamlCompilationOptions.Compile)]) Tag in .cs of the XAML
class file.
But unfortunately, We are unable to reproduce the “Cannot resolve property 'xxx'" warning” from our side, So Kindly provide More details in order to reproduce the issue.
Have you enabled any Style Cop or FxCop rule set in your application
? If yes, Mention those rule set details.
Type of property you have used to bind the Custom Entry Text Property.
SfDataGrid product version that you are currently using.
If possible kindly provide us the Issue replicating sample or modify the attached sample to replicate the issue.
We have prepared the sample as per your requirement, Do check and let us know Is there anything needs to add more to reproduce “Cannot resolve property 'xxx'" warning” issue.
Sample Link : http://www.syncfusion.com/downloads/support/directtrac/general/ze/DataGrid-1806700714
Regards,
Vigneshkumar R

What is the best way to create the MasterDetailPage menu items in Xamarin.Forms

I'm using a MasterDetailPage in my Xamarin.Forms application.
To creat the menu items, I use buttons
<MasterDetailPage.Master>
<ContentPage Title="Menu">
<StackLayout Orientation="Vertical">
<Button Text="Club-House" Command="{Binding ClubHouseCommand}" />
<Button Text="Parcours" />
<BoxView VerticalOptions="CenterAndExpand" />
<Button Text="A propos" Command="{Binding AProposCommand}" VerticalOptions="End" />
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
Is there a better way to do it ? I was thinking about something more menuish like MenuItem or else.
Thanks
There is nothing standard for implementing a menu in the Master page.
You can use
ListView
StackLayout
Grid
Or a combination. StackLayout and ListView will most likely be your best options. Personally I go for a StackLayout because it's quicker to code and render than a ListView, which may come with more issues than advantages. However if I have a large menu (which I try to avoid), ListView may be quicker to code. But ListViews are generally meant for dynamically loaded data, not static.
As for the elements, the most common will be
Button
Label
While buttons are designed for clicking, sometimes I move to label's if I don't want to have to undo all the default styling, and use the TapGestureRecognizer.
You can create a new User Control for a menu item, to avoid code replication.
All up, there is no standard. Make it easy to code and understand, and quick to render. How you do that, depends on your requirements.

Implementing UWP flyout splitview using masterdetailview in prism?

I am a complete noob to xamarin :)
So was wondering if someone could point me to right resources.
To be short, I want to implement something like this in prism using Xamarin MasterDetailPage.
Using the master detail sample here, the hamburger menu doesn't act as a fly out.
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="HelloWorld.Views.MyMasterDetail">
<MasterDetailPage.Master>
<ContentPage Title="Default">
<StackLayout>
<Button Text="ViewA" Command="{Binding NavigateCommand}" CommandParameter="MyNavigationPage/ViewA?id=A" />
<Button Text="ViewB" Command="{Binding NavigateCommand}" CommandParameter="MyNavigationPage/ViewB?id=B" />
<Button Text="ViewC" Command="{Binding NavigateCommand}" CommandParameter="MyNavigationPage/ViewC?id=C" />
</StackLayout>
</ContentPage>
</MasterDetailPage.Master>
</MasterDetailPage>
Right now, though there is enough space, it shows something like
(just as a sample, I am not using SplitViewMenu at all)
I need icons/some small text to show initially and on clicking hamburger, it should expand (you know just like the first link/ groove music app behavior).
Tips?
Right now, though there is enough space, it shows something like
In my experience, if you have assigned the Symbol property for SimpleNavMenuItem, the possible reason is you haven't imported the Themes file Generic.xaml under Themes folder
This file includes templates, styles for custom controls. For example, for NavMenuItem's template, FontIcon's Glyph property needs to be assigned correctly here:
<DataTemplate x:Key="NavMenuItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="48" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<FontIcon FontSize="16" Glyph="{Binding Path=SymbolAsChar}" VerticalAlignment="Center"
HorizontalAlignment="Center" ToolTipService.ToolTip="{Binding Path=Label}" />
<TextBlock Grid.Column="1" Text="{Binding Path=Label}" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
While SimpleNavMenuItem class's SymbolAsChar property is based on Symbol's value:
public sealed class SimpleNavMenuItem : INavigationMenuItem
{
......
public Symbol Symbol { get; set; }
public char SymbolAsChar => (char) Symbol;
......
}
And if you still can make it work, please share a demo:)
I've sort of been looking for something like this myself since I can't hide the NavigationBar shown in the Master page of the MasterDetailPage when i run it on a Windows 10 Mobile device (I can hide for PC though). So basically, I'm looking into eventually building my own version of the MasterDetailPage.
Since I have not built it yet, I can't tell you exactly how to achieve what you're asking, but I do know that it will require you to either:
Write your own custom renderer for MasterDetailView or,
Write a new control and its renderer
In both cases, your renderer will involve creating and manipulating a new SplitView (which is the native UWP control that your SplitViewMenu example is extending). There's a tutorial for creating the actual UWP control here. If you havent learned about Xamarin's renderers yet, they are the "Translator" and "Interpreter" between a Xamarin.Forms control and a given platform's native control. I suspect Xamarin will eventually rewrite their MastDetailPage renderer for UWP to use a SplitView as a base, but who knows when that will be. Xamarin also has an open source SDK for Xamarin.Forms (as well as the others) on GitHub so you can study the MasterDetailPageRenderer for UWP.

Resources