How do I add content to my app without it interfering with my navigation view? - xaml

I am very much a beginner with WinUI 3. I am learning and don't have much direction at all.
I'm trying to add a navigation bar to the left and then add content (buttons, text boxes, etc) to the right.
I don't know if I should add the navigation view to the grid or how I would do that. I have two code examples that I've tried and neither of them are doing what I want them to do.
An example of what I want my app to look like is the WinUI 3 Gallery app.
The navigation bar is added to the app as expected, but when I try to add the grid to start adding buttons and text boxes and other things, I get an error with this code:
<Window
x:Class="WinUI_3_with_Navigation_and_Grid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI_3_with_Navigation_and_Grid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<NavigationView x:Name="nvSample" PaneDisplayMode="Auto" Background="#c7cbd1">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Play" Content="Menu Item1" Tag="SamplePage1" />
<NavigationViewItem Icon="Save" Content="Menu Item2" Tag="SamplePage2" />
<NavigationViewItem Icon="Refresh" Content="Menu Item3" Tag="SamplePage3" />
<NavigationViewItem Icon="Download" Content="Menu Item4" Tag="SamplePage4" />
</NavigationView.MenuItems>
<Frame x:Name="contentFrame"/>
</NavigationView>
<Grid Background="Gray" ColumnDefinitions="50, Auto, *" RowDefinitions ="50, Auto, *">
<Rectangle Fill="Red" Grid.Column="0" Grid.Row="0" />
<Rectangle Fill="Blue" Grid.Row="1" />
<Rectangle Fill="Green" Grid.Column="1" />
<Rectangle Fill="Yellow" Grid.Row="1" Grid.Column="1" />
<Rectangle Fill="BlanchedAlmond" Grid.Row="2" Grid.Column="0"/>
<Rectangle Fill="DarkCyan" Grid.Row="2" Grid.Column="1"/>
<Rectangle Fill="MidnightBlue" Grid.Row="2" Grid.Column="2"/>
</Grid>
</Window>
Or I do this and it messes everything up...
<Window
x:Class="WinUI_3_with_Navigation_and_Grid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WinUI_3_with_Navigation_and_Grid"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Gray" ColumnDefinitions="50, Auto, *" RowDefinitions ="50, Auto, *">
<NavigationView x:Name="nvSample" PaneDisplayMode="Auto" Background="#c7cbd1">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Play" Content="Menu Item1" Tag="SamplePage1" />
<NavigationViewItem Icon="Save" Content="Menu Item2" Tag="SamplePage2" />
<NavigationViewItem Icon="Refresh" Content="Menu Item3" Tag="SamplePage3" />
<NavigationViewItem Icon="Download" Content="Menu Item4" Tag="SamplePage4" />
</NavigationView.MenuItems>
<Frame x:Name="contentFrame"/>
</NavigationView>
<Rectangle Fill="Red" Grid.Column="0" Grid.Row="0" />
<Rectangle Fill="Blue" Grid.Row="1" />
<Rectangle Fill="Green" Grid.Column="1" />
<Rectangle Fill="Yellow" Grid.Row="1" Grid.Column="1" />
<Rectangle Fill="BlanchedAlmond" Grid.Row="2" Grid.Column="0"/>
<Rectangle Fill="DarkCyan" Grid.Row="2" Grid.Column="1"/>
<Rectangle Fill="MidnightBlue" Grid.Row="2" Grid.Column="2"/>
</Grid>
</Window>

The error you are getting is because Window can have one content as a child. In your first block of code, the Window has a NavigationView and a Grid.
If you want something like the WinUI 3 Gallery, you can see the actual code in the GitHub repo.
You also should consider using the Template Studio for WinUI. You can implement navigation with few clicks.
Here's a sample code to understand how to implement a simple navigation.
Let's say we have 2 pages, Page1.xaml and Page2.xaml.
.xaml
<NavigationView SelectionChanged="NavigationView_SelectionChanged">
<NavigationView.MenuItems>
<NavigationViewItem Content="Page1" Tag="Page1"/>
<NavigationViewItem Content="Page2" Tag="Page2"/>
</NavigationView.MenuItems>
<Frame x:Name="ContentFrame"/>
</NavigationView>
.xaml.cs
namespace NavigationSampleApp;
private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
if (args.SelectedItem is NavigationViewItem item &&
item.Tag is string tag &&
Type.GetType($"NavigationSampleApp.{tag}") is Type pageType)
{
this.ContentFrame.Navigate(pageType);
}
}

Related

Custom Title bar showing up white-ish in WinUI 3

I'm back with another problem in my browser that I'm working on. I made a custom title bar where the Tabs come into the title bar to create a more immersive experience. but all that "Immersive experience" is being ruined by this seemingly unfixable bug. what ive tried to do is change the background of the CustomDragRegion Element.
look at the title bar. its disgusting. someone help me. plss
heres the code:
<Window
x:Class="Microsoft_Edge_WebView2_Runtime_Application.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft_Edge_WebView2_Runtime_Application"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d">
<Grid BorderBrush="Transparent" x:Name="Browser" Margin="0,0,0,0" Background="{ThemeResource SystemControlBackgroundAccentRevealBorderBrush}">
<TabView Margin="0, 0, 0, 0" x:Name="Tabv" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AddTabButtonClick="TabView_AddTabButtonClick" TabCloseRequested="TabView_TabCloseRequested">
<TabViewItem FontFamily="Gellix" x:Name="HomeTab" Header="Home" IsClosable="False">
<TabViewItem.IconSource>
<SymbolIconSource Symbol="Document" />
</TabViewItem.IconSource>
<Grid x:Name="TabContent" Margin="0,0,0,0" Background="#282828">
<WebView2 x:Name="WebView" Source="https://google.com" Margin="0,52,0,0"/>
<Grid Margin="0,10,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="666">
<TextBox KeyDown="AddressBar_KeyDown" FontFamily="Gellix" x:Name="AddressBar" HorizontalAlignment="Center" TextWrapping="NoWrap" Text="" VerticalAlignment="Center" Width="576" PlaceholderText="Search or type a URL here..." TextAlignment="Center" />
<Button Height="32" Width="40" x:Name="Home" Click="Button_Click_1">
<FontIcon FontFamily="Segoe Fluent Icons" Glyph="" Margin="-2,-1,-2,-1"/>
</Button>
<Button Height="32" Width="40" x:Name="Go" Margin="626,0,0,0" Click="Button_Click">
<FontIcon FontFamily="Segoe Fluent Icons" Glyph="" Margin="-2,-1,-2,-1"/>
</Button>
</Grid>
<Button x:Name="Refresh" Margin="0,10,10,0" VerticalAlignment="Top" Height="32" Width="40" FontFamily="Segoe UI Symbol" Click="Button_Click" HorizontalAlignment="Right">
<FontIcon FontFamily="Segoe Fluent Icons" Glyph="" Margin="-2,-1,-2,-1"/>
</Button>
<Button x:Name="Backward" Margin="10,10,0,0" VerticalAlignment="Top" Height="32" Width="40" FontFamily="Segoe UI Symbol" Click="Backward_Click">
<FontIcon FontFamily="Segoe Fluent Icons" Glyph="" Margin="-2,-1,-2,-1"/>
</Button>
<Button x:Name="Forward" Margin="53,10,0,0" VerticalAlignment="Top" Height="32" Width="40" FontFamily="Segoe UI Symbol" Click="Forward_Click">
<FontIcon FontFamily="Segoe Fluent Icons" Glyph="" Margin="-2,-1,-2,-1"/>
</Button>
</Grid>
</TabViewItem>
<TabView.TabStripFooter>
<Grid Margin="0,0,0,0" x:Name="CustomDragRegion" Background="Transparent"/>
</TabView.TabStripFooter>
</TabView>
</Grid>
if you really need c# code then just comment. cuz I have only two lines of c# related to the title bar.
In my case, I was able set the tittle bar to the color defined in the xaml by setting the WindowCaptionsBackground and WindowCaptionsBackgroundDisabled properties to Transparent, as such:
App.xaml
<Application
...
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Other app resources here -->
<SolidColorBrush x:Key="WindowCaptionBackground">Transparent</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">Transparent</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</Application>
More info on customizing the tittle bar: https://learn.microsoft.com/en-us/windows/apps/develop/title-bar?tabs=winui3

Bind to parent DataContext of TreeViewItem BUT TreeView's DataContext for top-most items

I have a TreeView that is using a single HierarchicalDataTemplate. I know how to bind to a property of the parent TreeViewItem in a HierarchicalDataTemplate:
<Button
Command="{Binding DataContext.RemoveCommand,
RelativeSource={RelativeSource AncestorLevel=2,
AncestorType=TreeViewItem}}"
CommandParameter="{Binding ElementId}" />
However, this does of course not work for the top-most items, since there is no ancestor TreeViewItem - here, I want to use the DataContext of the TreeView itself. How can I bind the command to the TreeView's DataContext for the top-most elements of the tree view? If necessary, you can assume that the view model has a property of the same name as the hierarchical view models representing each tree item.
This is my modified MainWindow.xaml based on the SubModelCollection example of Elmish.WPF (repo on GitHub), that demonstrates how I thought I solved the problem. As I mentioned in the comment to the OP, I encountered a new problem with this solution, but I hope it can be ironed out without too much effort. I have a feeling it's a trivial problem that remains.
My intention here is that one HierarchicalDataTemplate will be used for the top level items, and another for all other items. To my surprise it looks like it actually works as far as manipulating the data correctly, and the buttons work on all levels, but WPF is not happy with what's going on. Nodes on level two collapses visually when I add, remove or move subnodes below them, and the Output pane in VS has something to tell:
"System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'TreeViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
"
I had the same kind of error before introducing SharedPart, so I don't think that's causing it.
This is MainWindow.xaml, the only file I modified.
<Window
x:Class="Elmish.WPF.Samples.SubModelCollection.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Single counter" Height="800" Width="1000"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<DataTemplate x:Key="SharedPart">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CounterId}" Width="250" Margin="10,5,10,5" />
<TextBlock Text="{Binding CounterValue, StringFormat='Counter value: {0}'}" Width="100" Margin="0,5,10,5" />
<Button Command="{Binding Decrement}" Content="-" Margin="0,5,10,5" Width="30" />
<Button Command="{Binding Increment}" Content="+" Margin="0,5,10,5" Width="30" />
<Button Command="{Binding Reset}" Content="Reset" Margin="0,5,10,5" Width="50" />
<TextBlock Text="{Binding StepSize, StringFormat='Step size: {0}'}" Margin="0,5,10,5" />
<Slider Value="{Binding StepSize}" TickFrequency="1" Maximum="10" Minimum="1" IsSnapToTickEnabled="True" Width="100" Margin="0,5,10,5" />
<Button Command="{Binding AddChild}" Content="Add child" Margin="0,5,10,5" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="Level2Data" ItemsSource="{Binding Path=ChildCounters}">
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource SharedPart}"/>
<Button
Command="{Binding DataContext.Remove, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}, AncestorLevel=2}}"
CommandParameter="{Binding CounterId}"
Content="×" Margin="0,5,10,5" Width="20" />
<Button
Command="{Binding DataContext.MoveUp, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}, AncestorLevel=2}}"
CommandParameter="{Binding CounterId}"
Content="↑" Margin="0,5,10,5" Width="20" />
<Button
Command="{Binding DataContext.MoveDown, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}, AncestorLevel=2}}"
CommandParameter="{Binding CounterId}"
Content="↓" Margin="0,5,10,5" Width="20"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="Level1Data" ItemsSource="{Binding Path=ChildCounters}" ItemTemplate="{StaticResource Level2Data}">
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource SharedPart}"/>
<Button
Command="{Binding DataContext.Remove, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"
CommandParameter="{Binding CounterId}"
Content="×" Margin="0,5,10,5" Width="20" />
<Button
Command="{Binding DataContext.MoveUp, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"
CommandParameter="{Binding CounterId}"
Content="↑" Margin="0,5,10,5" Width="20" />
<Button
Command="{Binding DataContext.MoveDown, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"
CommandParameter="{Binding CounterId}"
Content="↓" Margin="0,5,10,5" Width="20"/>
</StackPanel>
</HierarchicalDataTemplate>
</Window.Resources>
<StackPanel Margin="0,20,0,10">
<Button Command="{Binding AddCounter}" Content="Add counter" Width="150" Margin="0,0,0,20" />
<TreeView ItemsSource="{Binding Counters}" ItemTemplate="{StaticResource Level1Data}">
</TreeView>
</StackPanel>

The splitview items aren't properly stacking beneath hamburger button

I am working on Visual Studio 2015 and trying to make a hamburger style navigation... but the items of the pane of the splitview are automatically getting a margin, and are not properly stacking beneath the hamburger button [look at the attached image]. I want them to properly stack beneath the Hamburger button on the leftmost of the grid.
I want to use the listbox so that when user navigates to a different page, it remains selected/highlighted, so I can't remove that.
I have attached the MainPage.xaml code and the code of the style i have used. Hope you would help .. Thanks!
<Page
x:Class="MathAssistant.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MathAssistant"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Name="HBbutton" Click="HBbutton_Click" Grid.Column="0" Grid.Row="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FontFamily="Segoe MDL2 Assets" Content="" FontSize="25" Background="BlueViolet"/>
<TextBlock Name="Heading" Grid.Column="1" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="36" Foreground="CornflowerBlue" />
<SplitView Grid.Row="1"
Grid.ColumnSpan="2"
Name="Menu"
DisplayMode="CompactOverlay"
OpenPaneLength="270"
CompactPaneLength="56">
<SplitView.Pane>
<ListBox SelectionMode="Single"
SelectionChanged="MenuListBox_SelectionChanged">
<ListBoxItem Name="MenuItemUnitConverter">
<StackPanel Orientation="Horizontal">
<Image Margin="0" Source="Assets/unitconverterlogo.png" Style="{StaticResource SplitviewLogoStyle}" />
<TextBlock FontSize="24" Margin="20,0,0,0">
<Run Text="Unit Converter"/>
</TextBlock>
</StackPanel>
</ListBoxItem>
<ListBoxItem Name="MenuItemCalculator" >
<StackPanel Orientation="Horizontal">
<Image Margin="0" Source="Assets/calculatorlogo.png" Style="{StaticResource SplitviewLogoStyle}"/>
<TextBlock FontSize="24" Margin="20,0,0,0">Calculator</TextBlock>
</StackPanel>
</ListBoxItem>
</ListBox>
</SplitView.Pane>
<SplitView.Content>
<Frame Name="MyFrame" Grid.Column="1" Grid.Row="1"></Frame>
</SplitView.Content>
</SplitView>
</Grid>
<Application.Resources>
<Style TargetType="Image" x:Key="SplitviewLogoStyle">
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="45" />
</Style>
ListBoxItem has a default padding (that's the opposite of a margin) of "12,11,12,13". Try setting the Padding of the ListBoxItems to 0 then it should be aligned to the left. To set it in the center you could do something like this:
<ListBoxItem Name="MenuItemUnitConverter" Padding="0">
<StackPanel Orientation="Horizontal">
<Image Margin="4" Source="Assets/unitconverterlogo.png" Style="{StaticResource SplitviewLogoStyle}" />
<TextBlock FontSize="24" Margin="20,0,0,0">
<Run Text="Unit Converter"/>
</TextBlock>
</StackPanel>
</ListBoxItem>
And modify the Style so that image's margin on left + width + image's margin on the right = SplitView.Width (4+48+4=56):
<Style TargetType="Image" x:Key="SplitviewLogoStyle">
<Setter Property="Height" Value="50" />
<Setter Property="Width" Value="48" />
</Style>

XAML fixed banner on top of scrollable area

I'm trying to add a banner that remains fixed in the window to my page and I'm having a tough time.
This is what I am trying to achieve: I want a banner that floats at the top of the window (for an ad), then I want the rest of the content in a scrollable area. First item should be a textBlock, then a textBox, then a button.
This is the code I've got now and it looks right except for the scrolling. Help would be appreciated.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Universal="using:Microsoft.AdMediator.Universal"
x:Class="App2.MainPage"
mc:Ignorable="d" RequestedTheme="Dark">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel Grid.RowSpan="3">
<Universal:AdMediatorControl x:Name="AdMediatorName" Height="90" Id="AdMediator-Id" Margin="10,0"/>
<ScrollViewer x:Name="myScrollViewer" VerticalScrollMode="Enabled">
<StackPanel Grid.RowSpan="2">
<TextBlock x:Name ="outputConsole" FontSize="15" RenderTransformOrigin="0.5,0" TextWrapping="WrapWholeWords" Margin="0,0,10,0" FontFamily="Consolas" IsTextSelectionEnabled="True">
<TextBlock.RenderTransform>
<CompositeTransform/>
</TextBlock.RenderTransform>
<TextBlock.Projection>
<PlaneProjection/>
</TextBlock.Projection>
<Run/>
<LineBreak/>
<Run/>
</TextBlock>
<TextBox x:Name="inputConsole" FontSize="20" KeyUp="inputKeyUp" Margin="0,0,10,0" FontFamily="Consolas" IsTapEnabled="True" IsTextPredictionEnabled="True"/>
<Button x:Name="submitButton" Content="Submit" Click="submitButtonClick"/>
</StackPanel>
</ScrollViewer>
</StackPanel>
</Grid>
</Page>
I figured it out. I just needed to put it directly in the grid. Sorry, still learning XAML.
You just answered your question. You have to do something like this,
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal"><TextBlock x:Name ="outputConsole" FontSize="15" RenderTransformOrigin="0.5,0" TextWrapping="WrapWholeWords" Margin="0,0,10,0" FontFamily="Consolas" IsTextSelectionEnabled="True">
<TextBlock.RenderTransform>
<CompositeTransform/>
</TextBlock.RenderTransform>
<TextBlock.Projection>
<PlaneProjection/>
</TextBlock.Projection>
<Run/>
<LineBreak/>
<Run/>
</TextBlock>
<TextBox x:Name="inputConsole" FontSize="20" KeyUp="inputKeyUp" Margin="0,0,10,0" FontFamily="Consolas" IsTapEnabled="True" IsTextPredictionEnabled="True"/>
<Button x:Name="submitButton" Content="Submit" Click="submitButtonClick"/>
</StackPanel>
<ScrollViewer Grid.Row="1">
</ScrollViewer>

Windows phone 8 app bar command parameter always null - Cimbalino

Xaml Namespaces
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:appBar="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP8"
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"
Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Text="Beneficiary"
Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<toolkit:LongListMultiSelector
x:Name="beneficoryList"
ItemsSource="{Binding BeneficoryCollection}"
EnforceIsSelectionEnabled="{Binding DataContext.IsSelectionEnabled, ElementName=LayoutRoot,Mode=TwoWay}">
<!--Is Selection Enabled Changed Command-->
<!--<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="IsSelectionEnabledChanged">
<interactivity:InvokeCommandAction Command="{Binding DataContext.IsSelectionEnabledChangedOn,ElementName=LayoutRoot,Mode=OneTime}"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>-->
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
<TextBlock Text="{Binding Number}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
<TextBlock Text="{Binding Operator}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
<!-- Interaction region-->
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<interactivity:InvokeCommandAction Command="{Binding TapCommand}" CommandParameter="{Binding }"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
<!-- Interaction region ends-->
</StackPanel>
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
</Grid>
</Grid>
<interactivity:Interaction.Behaviors>
<appBar:ApplicationBarBehavior x:Name="appBar" IsVisible="True"
BackgroundColor="{StaticResource PhoneAccentColor}"
ForegroundColor="{StaticResource PhoneBackgroundColor}" Mode="Default">
<appBar:ApplicationBarIconButton Text="Add"
IconUri="/Toolkit.Content/ApplicationBar.Add.png"
IsVisible="{Binding IsAddCommandVisible,Mode=TwoWay}"
Command="{Binding AddCommand}"/>
<appBar:ApplicationBarIconButton Text="Delete"
IconUri="/Toolkit.Content/ApplicationBar.Delete.png"
Command="{Binding DeleteCommand,Mode=OneTime}"
CommandParameter="{Binding SelectedItems,ElementName=beneficoryList}"/>
<!--Menu Items-->
<appBar:ApplicationBarBehavior.MenuItems>
<appBar:ApplicationBarMenuItem Text="HelpDesk" />
<appBar:ApplicationBarMenuItem Text="Contact Us"/>
</appBar:ApplicationBarBehavior.MenuItems>
<!--Menu Items End-->
</appBar:ApplicationBarBehavior>
</interactivity:Interaction.Behaviors>
View Model
private RelayCommand<object> _ondeleteCommand;
public RelayCommand<object> DeleteCommand
{
get { return _ondeleteCommand ?? (_ondeleteCommand = new RelayCommand<object>(OnDeleteCommand)); }
}
private void OnDeleteCommand(object tobeDeleted)
{
if (tobeDeleted != null)
{
// Delete
}
}
How do I pass selectedItems of LongListMultiSelector to a command .In the command argument I always getting null. there is no binding errors in the output window.
Cimbalino Guide
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:appBar="clr-namespace:Cimbalino.Phone.Toolkit.Behaviors;assembly=Cimbalino.Phone.Toolkit"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP8"
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot"
Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--App Bar Binding-->
<interactivity:Interaction.Behaviors>
<appBar:ApplicationBarBehavior x:Name="appBar" IsVisible="True"
BackgroundColor="{StaticResource PhoneAccentColor}"
ForegroundColor="{StaticResource PhoneBackgroundColor}" Mode="Default">
<appBar:ApplicationBarIconButton Text="Add"
IconUri="/Toolkit.Content/ApplicationBar.Add.png"
IsVisible="{Binding IsAddCommandVisible,Mode=TwoWay}"
Command="{Binding AddCommand}"/>
<appBar:ApplicationBarIconButton Text="Delete"
IconUri="/Toolkit.Content/ApplicationBar.Delete.png"
Command="{Binding DeleteCommand,Mode=OneTime}"
CommandParameter="{Binding SelectedItems,ElementName=beneficoryList}"/>
<!--Menu Items-->
<appBar:ApplicationBarBehavior.MenuItems>
<appBar:ApplicationBarMenuItem Text="HelpDesk" />
<appBar:ApplicationBarMenuItem Text="Contact Us"/>
</appBar:ApplicationBarBehavior.MenuItems>
<!--Menu Items End-->
</appBar:ApplicationBarBehavior>
</interactivity:Interaction.Behaviors>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel"
Grid.Row="0"
Margin="12,17,0,28">
<TextBlock Text="Beneficiary"
Margin="9,-7,0,0"
Style="{StaticResource PhoneTextTitle1Style}" />
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel"
Grid.Row="1"
Margin="12,0,12,0">
<toolkit:LongListMultiSelector
x:Name="beneficoryList"
ItemsSource="{Binding BeneficoryCollection}"
EnforceIsSelectionEnabled="{Binding DataContext.IsSelectionEnabled, ElementName=LayoutRoot,Mode=TwoWay}">
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
<TextBlock Text="{Binding Number}" FontSize="{StaticResource PhoneFontSizeMedium}"/>
<TextBlock Text="{Binding Operator}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
<!-- Interaction region-->
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<interactivity:InvokeCommandAction Command="{Binding TapCommand}" CommandParameter="{Binding }"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
<!-- Interaction region ends-->
</StackPanel>
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
</Grid>
</Grid>
The element 'beneficoryList' is not finding in the visual tree, I was placed the app bar outside of the grid , Now I moved the App-bar inside the grid so 'beneficoryList' is now selectedItems is passed to VM . I think WP only allow traversing to just its immediate parent