Binding ContextMenu defined in a DataTemplate to command - contextmenu

Ok, here is my problem: I have a UserControl Template defined in Style.xaml like this
<Style x:Key="ModulProfTemplate" TargetType="{x:Type UserControl}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type UserControl}">
<Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" BorderThickness="{TemplateBinding BorderThickness}" DataContext="{Binding DataContext, RelativeSource={RelativeSource TemplatedParent}}">
<Border x:Name="border" BorderBrush="#004E6CA9" BorderThickness="1.5">
<Grid x:Name="Layout" Background="{Binding Background, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Margin="0.5" DataContext="{Binding}">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Disponibil" Command="{Binding GridContextMenuCommand}" CommandParameter="disp"/>
<MenuItem Header="Indisponibil" Command="{Binding GridContextMenuCommand}" CommandParameter="indisp"/>
</ContextMenu>
</Grid.ContextMenu>
<Grid.RowDefinitions>
<RowDefinition Height="0.497*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" d:LayoutOverrides="Height" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock TextWrapping="Wrap" Text="{Binding SalaSaptPara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="{Binding MaterieSaptPara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock TextWrapping="Wrap" Text="{Binding SalaSaptImpara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text="{Binding MaterieSaptImpara, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2,0,0,0"/>
</StackPanel>
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="#CA4E6CA9"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
the problem is that i don't know how to bind the command to menuitem. The UserControl's DataContext is set to ViewModel.Luni.M1, and the command is in ViewModel DataContext. So my question is: How do i change the DataContext so i can bind the command?

Solved, all i had to do was add a tag to the border that was bound to my viewmodel's datacontext, and after that use PlacementTarget.Tag.MyCommand, as shown like this :)
<Border x:Name="border" BorderBrush="#004E6CA9" BorderThickness="1.5" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:CadreDidacticeView, AncestorLevel=1}, Path=DataContext}">
<Border.ContextMenu>
<ContextMenu>
<MenuItem Header="Disponibil" Command="{Binding PlacementTarget.Tag.GridContextMenuCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}">
</MenuItem>
</ContextMenu>
</Border.ContextMenu>

Related

AppShell tabbar color changes on iOS when there is content behind it

I have the exact same issue as asked in this question. The tabbar changes color when there is content behind it. However, as stated in this question, if they remove the renderer then their problem stops. I currently do not have a renderer at all and still have this problem. The only part of my code that doesn't have this problem are pages that are not in the shell hierarchy and that are navigated to using the following line of code:
var search = new SearchList();
Navigation.PushAsync(search);
The pages that are navigated to using the style of the following lines of code have the tabbar issue:
await Shell.Current.GoToAsync($"//{nameof(MemberList)}");
The xaml and code behind for the different pages are the exact same. The only difference is one is defined in the appshell hierarchy(MemberList) and the other is not(SearchList) as seen below:
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:SampleYE.Views"
Title="SampleYE"
x:Class="SampleYE.AppShell"
>
<!--
The overall app visual hierarchy is defined here, along with navigation.
https://learn.microsoft.com/xamarin/xamarin-forms/app-fundamentals/shell/
-->
<Shell.Resources>
<ResourceDictionary>
<Style x:Key="BaseStyle" TargetType="Element">
<Setter Property="Shell.BackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.ForegroundColor" Value="{StaticResource Secondary}" />
<Setter Property="Shell.TitleColor" Value="{StaticResource Secondary}" />
<Setter Property="Shell.DisabledColor" Value="#AD94BB" />
<Setter Property="Shell.UnselectedColor" Value="{StaticResource Tertiary}" />
<Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource Primary}" />
<Setter Property="Shell.TabBarForegroundColor" Value="{StaticResource Secondary}"/>
<Setter Property="Shell.TabBarUnselectedColor" Value="{StaticResource Tertiary}"/>
<Setter Property="Shell.TabBarTitleColor" Value="{StaticResource Secondary}"/>
</Style>
<Style TargetType="TabBar" BasedOn="{StaticResource BaseStyle}" />
<Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" />
</ResourceDictionary>
</Shell.Resources>
<TabBar>
<ShellContent Route="StartupPage" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:StartupPage}" />
</TabBar>
<TabBar>
<Tab Title="Directory" Icon="icon_feed.png">
<ShellContent Route="MemberList" ContentTemplate="{DataTemplate local:MemberList}"/>
</Tab>
<Tab Title="Profile" Icon="icon_about.png">
<ShellContent Route="ProfilePage1" ContentTemplate="{DataTemplate local:ProfilePage1}" />
</Tab>
</TabBar>
<!--
If you would like to navigate to this content you can do so by calling
await Shell.Current.GoToAsync("//LoginPage");
-->
<TabBar>
<ShellContent Route="LoginPage1" Shell.FlyoutBehavior="Disabled" ContentTemplate="{DataTemplate local:LoginPage1}" />
</TabBar>
</Shell>
The definition of the pages with the issue are as follows:
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SampleYE.Views.MemberList">
<ContentPage.Content>
<ListView x:Name="listView" ItemSelected="OnItemSelected" IsGroupingEnabled="true">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
<Label Text="{Binding Title}" VerticalTextAlignment="Center" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Image WidthRequest="44" HeightRequest="44" Source="{Binding Photo}" />
<StackLayout Padding="5,0,0,0" VerticalOptions="StartAndExpand" Orientation="Vertical">
<Label Text="{Binding Name}" VerticalTextAlignment="Center" Font="Medium" />
<Label Text="{Binding Title}" VerticalTextAlignment="Center" Font="Micro" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
<ContentPage.Content>
<Grid RowDefinitions="Auto">
<ScrollView Grid.Row="0" >
<Grid RowDefinitions="Auto,Auto,Auto,Auto" ColumnDefinitions="*,*" VerticalOptions="CenterAndExpand"
RowSpacing="25">
<Frame Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" BorderColor="{StaticResource Secondary}"
VerticalOptions="Start" HorizontalOptions="Center" WidthRequest="150" HeightRequest="150"
CornerRadius="75" HasShadow="False" Padding="0" IsClippedToBounds="True">
<Image Source="Profile" Aspect="AspectFill"/>
</Frame>
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
TextColor="{StaticResource Primary}" BackgroundColor="{StaticResource Secondary}"
HeightRequest="54" WidthRequest="54" CornerRadius="27" TranslationX="65"
ImageSource="Camera" HorizontalOptions="Center" VerticalOptions="End"/>
<Frame Grid.Row="2" Grid.ColumnSpan="2" IsClippedToBounds="True"
Margin="20,20,20,0" CornerRadius="15" Padding="0,20,0,20" >
<Grid RowDefinitions="*,*,*" ColumnDefinitions="*,*,*,*" RowSpacing="12" ColumnSpacing="5">
<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Calvin"/>
<Label Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Carter"/>
<Label Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Ace"/>
<Label Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Wrecking Ball"/>
<Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Spring"/>
<Label Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="2014"/>
</Grid>
</Frame>
<Frame Grid.Row="3" Grid.ColumnSpan="2" IsClippedToBounds="False"
Margin="20,20,20,0" CornerRadius="15" Padding="0,20,0,45" >
<Grid RowDefinitions="*,Auto,Auto,*" ColumnDefinitions="*,*,*,*" RowSpacing="20" ColumnSpacing="5" >
<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4" HorizontalTextAlignment="Center"
Text="02/01/1994"/>
<Label Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" HorizontalTextAlignment="Center"
Text="Wreckingball14"/>
<Label Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="CCC#blackhousedevelopers.com"/>
<Label Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Wrecking Ball"/>
<Label Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="Spring"/>
<Label Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="2" HorizontalTextAlignment="Center"
Text="2014"/>
</Grid>
</Frame>
<Grid
Grid.Row="4" Grid.ColumnSpan="2" IsClippedToBounds="False"
Margin="20,0,20,0" Padding="0,20,0,20"
RowDefinitions="Auto,Auto" ColumnDefinitions="*,*" RowSpacing="20" >
<Frame IsClippedToBounds="true" Grid.Row="0" Grid.ColumnSpan="2"
Padding="0"
CornerRadius="15">
<Editor HeightRequest="70" x:Name="interestEntry"
BackgroundColor="{StaticResource Primary}" TextColor="{StaticResource Secondary}"
Placeholder="Interests (i.e. UFC, Fishing, Investing etc;)"
PlaceholderColor="{StaticResource Secondary}"/>
</Frame >
<Frame IsClippedToBounds="true" Grid.Row="1" Grid.ColumnSpan="2"
Padding="0"
CornerRadius="15">
<Editor HeightRequest="140" x:Name="bioEntry"
BackgroundColor="{StaticResource Primary}" TextColor="{StaticResource Secondary}"
Placeholder="Biography (A little information about yourself)"
PlaceholderColor="{StaticResource Secondary}"/>
</Frame>
</Grid>
</Grid>
</ScrollView>
</Grid>
</ContentPage.Content>
The fix to this was updating my ios project to a newer version. they fixed it in the update.

ListBox in Grid not resize in SplitView

I have problem with resize gridlayout.
This is man page:
This page has ListBox in 3 column. But when I fill in the data my ListBox I don't see my buttons and scrollbar. I have ScrollBar in ScrollViewer but it doesn't work. I generate all values in For loop (100 items). For now I have something like this:
This is my code of page:
<Page
x:Class="MemoryWords.LernWords"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MemoryWords"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="mainPage">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="4*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width=".5*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Name="WordBeforeTranslation"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20"/>
<StackPanel Grid.Column="1" >
<TextBlock Name="WordAfterTranslation"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20"/>
<TextBlock Name="WordArticulation"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20"/>
</StackPanel>
<ScrollViewer Grid.Column="2">
<ListBox Name="AllWords"/>
</ScrollViewer>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
Content="Previous"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<Button Grid.Column="1"
Content="Check"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<Button Grid.Column="2"
Content="Next"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
Name="BtnLoadFile"
Click="BtnLoadFile_Click"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="Load file"/>
<Button Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="New Random"/>
<Button Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="--------"/>
</Grid>
</Grid>
</Page>
Next case:
How I can set on starting, my grid filled all content of page without loaded data to ListBox? And Can I resize my app without lost of content? I don't want set permanent values to height and width. I would like have only layouts without permanent values. Generally, I would like when I will rescale app, my grid will by have similar layout.
I would like to make my page look like as on first my screenshot.
Is it possible?
EDIT:
This is my code from MainPage:
<Page
x:Class="MemoryWords.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MemoryWords"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<CommandBar HorizontalAlignment="Left">
<AppBarButton Label="Menu" Click="ToogleMenu_Click">
<AppBarButton.Icon>
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph=""/>
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton Icon="Back" Label="Back" Click="GoBack_Click"/>
<AppBarButton Icon="Home" Label="Play" Click="GoHome_Click"/>
</CommandBar>
<!--<Button Content="Open" Click="Button_Click"/>-->
<SplitView Name="MySplitView"
DisplayMode="CompactOverlay"
CompactPaneLength="50"
OpenPaneLength="200">
<SplitView.Pane >
<StackPanel>
<ListBox SelectionMode="Single"
Name="ListOfMenu"
SelectionChanged="ListOfMenu_SelectionChanged">
<ListBoxItem Name="LearnWordsItem">
<StackPanel Orientation="Horizontal">
<TextBlock FontFamily="Segoe MDL2 Assets"
Text=""
FontSize="30"
MinWidth="50"/>
<TextBlock Text="Nauka słówek"/>
</StackPanel>
</ListBoxItem>
<ListBoxItem Name="SettingItem">
<StackPanel Orientation="Horizontal">
<TextBlock FontFamily="Segoe MDL2 Assets"
Text=""
FontSize="30"
MinWidth="50"/>
<TextBlock Text="Ustawienia"/>
</StackPanel>
</ListBoxItem>
</ListBox>
</StackPanel>
</SplitView.Pane>
<SplitView.Content>
<StackPanel>
<Frame Name="MyFrame">
</Frame>
</StackPanel>
</SplitView.Content>
</SplitView>
</StackPanel>
</Page>
I saw that you've set three RowDefinition as "*", it certainly will drive out those buttons. If you want to make these buttons always show there, you need to set fixed height for the two rows. See the following code:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="100" />
<RowDefinition Height="100" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0"
Name="WordBeforeTranslation"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20" />
<StackPanel Grid.Column="1">
<TextBlock Name="WordAfterTranslation"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20" />
<TextBlock Name="WordArticulation"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="20" />
</StackPanel>
<ScrollViewer Grid.Column="2">
<ListBox Name="AllWords" />
</ScrollViewer>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
Content="Previous"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Button Grid.Column="1"
Content="Check"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
<Button Grid.Column="2"
Content="Next"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Grid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
Name="BtnLoadFile"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="Load file" />
<Button Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="New Random" />
<Button Grid.Column="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="--------" />
</Grid>
</Grid>
Ok, I found answer on my question. :) Yeeee :)
My mistake was that in mainPage I didn't contained all in the grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<CommandBar Grid.Row="0" HorizontalAlignment="Left">
<AppBarButton Label="Menu" Click="ToogleMenu_Click">
<AppBarButton.Icon>
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph=""/>
</AppBarButton.Icon>
</AppBarButton>
<AppBarButton Icon="Back" Label="Back" Click="GoBack_Click"/>
<AppBarButton Icon="Home" Label="Play" Click="GoHome_Click"/>
</CommandBar>
<SplitView Grid.Row="1" Name="MySplitView"
DisplayMode="CompactOverlay"
CompactPaneLength="50"
OpenPaneLength="200"
VerticalAlignment="Stretch">
<SplitView.Pane >
<StackPanel>
<ListBox SelectionMode="Single"
Name="ListOfMenu"
SelectionChanged="ListOfMenu_SelectionChanged">
<ListBoxItem Name="LearnWordsItem">
<StackPanel Orientation="Horizontal">
<TextBlock FontFamily="Segoe MDL2 Assets"
Text=""
FontSize="30"
MinWidth="50"/>
<TextBlock Text="Nauka słówek"/>
</StackPanel>
</ListBoxItem>
<ListBoxItem Name="SettingItem">
<StackPanel Orientation="Horizontal">
<TextBlock FontFamily="Segoe MDL2 Assets"
Text=""
FontSize="30"
MinWidth="50"/>
<TextBlock Text="Ustawienia"/>
</StackPanel>
</ListBoxItem>
</ListBox>
</StackPanel>
</SplitView.Pane>
<SplitView.Content>
<Frame Name="MyFrame" >
</Frame>
</SplitView.Content>
</SplitView>
</Grid>
Enough to change StackPanel on Grid. That is all.

Trouble finding ViewModel property from ListView

I have a rather complicated data binding template and I'm not able to find away back to my ViewModel to access a property and command.
This is how my xaml is set up from Top to Bottom as its layout:
<HierarchicalDataTemplate x:Key="ChapterReferencesTemplate">
<StackPanel>
<DockPanel>
<TextBlock Text="Chapter Reference "/>
<AccessText Text="{Binding Path=Chapter}" />
</DockPanel>
<DockPanel>
<TextBlock Text="Total Reference Verses "/>
<AccessText Text="{Binding Path=Verses}" />
</DockPanel>
<ListView Name="VerseReferencesListView" Height="200"
ItemsSource="{Binding Path=VerseReferences}"
SelectedItem="{Binding Path=DataContext.CurrentVerseReference, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type src:CreateWritingsViewModel}}}"
AlternationCount="2"
BorderThickness="0"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView AllowsColumnReorder="true" ColumnHeaderToolTip="xmlNamespace List" >
<GridViewColumn
Header="Verse"
DisplayMemberBinding="{Binding Path=Verse}" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" TextAlignment="Left" Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn
Header="Query"
DisplayMemberBinding="{Binding Path=Query}" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" TextAlignment="Left" Text="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Actions">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button
Command="{Binding Path=DataContext.LookupReferencesCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type src:CreateWritingsViewModel}}}"
CommandParameter="{Binding .}"
Template="{StaticResource AddButtonTemplate}" Cursor="Hand" Width="30"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
<HierarchicalDataTemplate x:Key="WritingsReferenceTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="200"/>
</Grid.ColumnDefinitions>
<GroupBox Header="{Binding Path=Hebrew}">
<StackPanel Margin="5 10 5 0">
<StackPanel>
<DockPanel>
<TextBlock Text="Writings of "/>
<AccessText Text="{Binding Path=Writings}" />
</DockPanel>
<DockPanel>
<TextBlock Text="Total Reference Chapters : "/>
<AccessText Text="{Binding Path=Chapters}" />
</DockPanel>
<DockPanel>
<TextBlock Text="Total Reference Verses : "/>
<AccessText Text="{Binding Path=Verses}" />
</DockPanel>
<DockPanel >
<TextBlock Text="Query for Writing : "/>
<AccessText Text="{Binding Path=QueryWriting}" />
</DockPanel>
<DockPanel >
<TextBlock Text="Query for Chapters : "/>
<AccessText Text="{Binding Path=QueryChapters}" />
</DockPanel>
</StackPanel>
<DockPanel Margin="0 5 0 0">
<GroupBox Header="Chapter References">
<Expander>
<ScrollViewer VerticalScrollBarVisibility="Auto" Height="200">
<HeaderedItemsControl
ItemTemplate="{StaticResource ChapterReferencesTemplate}"
ItemsSource="{Binding Path=ChapterReferences}"
Margin="10,0,0,0" />
</ScrollViewer>
</Expander>
</GroupBox>
</DockPanel>
</StackPanel>
</GroupBox>
</Grid>
<GroupBox Grid.Column="1" Header="CREATE REFERENCES">
<ListBox
Name="ReferenceListBox"
ItemTemplate="{StaticResource WritingsReferenceTemplate}"
ItemsSource="{Binding Path=odsDocumentsService.WritingsReferenceItems}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard">
</ListBox>
In my HierarchicalDataTemplate x:Key="ChapterReferencesTemplate" as you can see I'm already binding to ItemsSource="{Binding Path=VerseReferences}" but I also need to bind to my ViewModel's CurrentVerseReference for the SelectedItem property of the ListView.
Same for my Button Command to my ViewModel's LookupReferencesCommand. All the data is coming from bindings to a object class that contains a ObservableCollection (ChapterReferences, VerseReferences) So far I'm having no success in getting this to work and would appreciate your help very much.
Thanks!...
Well, I found the right properties to use to get this to work. But, I need to do some reading to find out more about these property settings. For my SelectedItem I had to do this:
SelectedItem="{Binding Path=DataContext.CurrentVerseReference, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
And for my Button Command and CommandParameter I had to do this and this is where I need to do more reading to understand how this worked:
<Button Command="{Binding Path=DataContext.LookupReferencesCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, AncestorLevel=2, Mode=FindAncestor}}"
CommandParameter="{Binding Path=DataContext.CurrentVerseReference, RelativeSource={RelativeSource AncestorType={x:Type ListBox}, AncestorLevel=2, Mode=FindAncestor}}"
Template="{StaticResource AddButtonTemplate}" Cursor="Hand" Width="30"/>
Its the AncestorLevel I don't understand. I'm assuming its because I have the Button control at the second level of the ListView?

Set the datacontext of the context menu with in a data template

Below is the code snippet:
<ResourceDictionary>
* * *
<DataTemplate x:Key="xyzTemplate" DataType="arguments" PresentationTraceSources.TraceLevel="High">
<StackPanel Orientation="Horizontal">
<TextBlock Text="(" VerticalAlignment="Center" Margin="2,0,2,0" FontSize="18"
x:Name="firstbrace" Visibility="Collapsed">
</TextBlock>
<ItemsControl ItemsSource="{Binding}" KeyboardNavigation.IsTabStop="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock
FontSize="11" VerticalAlignment="Bottom" Margin="1,0,1,2" Text=" , " Width="auto" Name="Comma" Height="auto" >
</TextBlock>
<ComboBox x:Name="blockCombo" Visibility="Collapsed" ItemsSource="{Binding XPath=.//value}" >
<ComboBox.ContextMenu>
<ContextMenu x:Name="xyzcontextMenu"
DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.DataContext}"
>
<MenuItem
Command="{Binding Path=xyzcommand}"
Header="MenuItem1" >
</MenuItem>
</ContextMenu>
</ComboBox.ContextMenu>
</ComboBox>
I need to set the data context of the context menu "xyzcontextmenu" as the data context of the UserControl.
Please let me know asap how can I achieve this
Thanks,
Shwetha

XAML:How to use a Binding Converter within a Style?

I used the Converter in a Sytle,but it seems that the Converter was never been called...
The code is like this:
<Style x:Key="DependencyToolTipStyle" TargetType="{x:Type ToolTip}">
<Style.Resources>
<local:ConvertTypeToTypeCN x:Key="Converter1"></local:ConvertTypeToTypeCN>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border Background="LightYellow" BorderThickness="1" BorderBrush="Black">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Margin="1" Text="type:" Grid.Column="0" Grid.Row="0" />
<TextBlock Margin="1" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="0">
<TextBlock.Text>
**<Binding Path="LinkType" Converter="{StaticResource Converter1}"/>**
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="1" Text="Lag:" Grid.Column="0" Grid.Row="1" />
<TextBlock Margin="1" Grid.Column="1" Grid.Row="1" Text="{Binding Lag}" />
<TextBlock Margin="1" Text="From:" Grid.Column="0" Grid.Row="2" />
<TextBlock Margin="1" Grid.Column="1" Grid.Row="2" Text="{Binding Path=From.ID}" />
<TextBlock Margin="1" Grid.Column="2" Grid.Row="2" Text="{Binding Path=From.Name}" />
<TextBlock Margin="1" Text="To:" Grid.Column="0" Grid.Row="3" />
<TextBlock Margin="1" Grid.Column="1" Grid.Row="3" Text="{Binding Path=To.ID}" />
<TextBlock Margin="1" Grid.Column="2" Grid.Row="3" Text="{Binding Path=To.Name}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ToolTip x:Key="DependencyToolTip" Style="{StaticResource DependencyToolTipStyle}"/>
<Style TargetType="controls:GanttDependency">
<Setter Property="ToolTip" Value="{StaticResource DependencyToolTip}" />
</Style>
Try defining the converter as a resource of the grid instead.
<Style x:Key="DependencyToolTipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Border Background="LightYellow" BorderThickness="1" BorderBrush="Black">
<Grid>
**<Grid.Resources>
<local:ConvertTypeToTypeCN x:Key="Converter1"></local:ConvertTypeToTypeCN>
</Grid.Resources>**
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Margin="1" Text="type:" Grid.Column="0" Grid.Row="0" />
<TextBlock Margin="1" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="0">
<TextBlock.Text>
**<Binding Path="LinkType" Converter="{StaticResource Converter1}"/>**
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="1" Text="Lag:" Grid.Column="0" Grid.Row="1" />
<TextBlock Margin="1" Grid.Column="1" Grid.Row="1" Text="{Binding Lag}" />
<TextBlock Margin="1" Text="From:" Grid.Column="0" Grid.Row="2" />
<TextBlock Margin="1" Grid.Column="1" Grid.Row="2" Text="{Binding Path=From.ID}" />
<TextBlock Margin="1" Grid.Column="2" Grid.Row="2" Text="{Binding Path=From.Name}" />
<TextBlock Margin="1" Text="To:" Grid.Column="0" Grid.Row="3" />
<TextBlock Margin="1" Grid.Column="1" Grid.Row="3" Text="{Binding Path=To.ID}" />
<TextBlock Margin="1" Grid.Column="2" Grid.Row="3" Text="{Binding Path=To.Name}" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ToolTip x:Key="DependencyToolTip" Style="{StaticResource DependencyToolTipStyle}"/>
<Style TargetType="controls:GanttDependency">
<Setter Property="ToolTip" Value="{StaticResource DependencyToolTip}" />
</Style>

Resources