I'm having a working datatemplate for a ListView with ItemTemplate
<ListView ItemTemplate="{StaticResource MYTEMPLATE}"
HorizontalAlignment="Center"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ContinuumNavigationTransitionInfo.IsEntranceElement="True">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VerticalAlignment="Bottom"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
The Template looks like that:
<DataTemplate x:Key="GlobalBox">
<Border Background="#FFFFFFFF" Margin="10 0 0 5" CornerRadius="2 2 15 2">
<Grid Width="380">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Tag="{Binding ID}" Name="ProfileInfo" Tapped="Profile_Tapped" Orientation="Horizontal" Margin="15 15 15 0">
<Grid Width="360" Margin="0 0 0 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Height="45" Width="45" CornerRadius="5">
<Border.Background>
<ImageBrush ImageSource="{Binding ImagePath}" Stretch="UniformToFill"/>
</Border.Background>
</Border>
<StackPanel Grid.Column="1" Orientation="Vertical" Margin="0 5 0 0">
<TextBlock Text="{Binding Name}" Foreground="Black" FontSize="18" FontWeight="Bold"></TextBlock>
<TextBlock Text="{Binding Handle}" Foreground="DarkGray" FontSize="12"/>
</StackPanel>
<Image Grid.Column="2" Source="Assets/ActionIcons/logo_blue_32.png" Width="32" VerticalAlignment="Top"></Image>
</Grid>
</StackPanel>
<StackPanel Grid.Row="1" Margin="14.5,0,0,0" Height="Auto">
<StackPanel Name="TweetContent" Tag="{Binding ID}" Margin="15 0 15 0" Tapped="Content_Tapped">
<TextBlock Text ="{Binding Content}" TextWrapping="Wrap" Foreground="Black" FontSize="14" Margin="0 0 0 10"/>
<ItemsControl ItemsSource="{Binding ContentImages}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding }" MaxWidth="350" Margin="0 0 0 5" HorizontalAlignment="Center"></Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<TextBlock Foreground="DarkGray" Text="{Binding DateSend}" FontSize="10"></TextBlock>
</StackPanel>
<StackPanel Name="ActionButtons">
<Grid Tag="{Binding ID}" Width="380" Height="25" Margin="0 0 0 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" HorizontalAlignment="Center" Margin="20 0 0 0" Style="{StaticResource replyActionButton}" Tapped="Reply_Tapped"></Button>
<ToggleButton Grid.Column="2" HorizontalAlignment="Center"
Style="{StaticResource retweetActionButton}"
Tapped="Retweet_Tapped"></ToggleButton>
<TextBlock Grid.Column="3" HorizontalAlignment="Left" Margin="-15 0 0 0" VerticalAlignment="Center" Text="{Binding RetweetCount}" Foreground="DarkGray"/>
<ToggleButton Grid.Column="4" HorizontalAlignment="Center"
Style="{StaticResource likeActionButton}"
IsChecked="{Binding LikeState}"
Tapped="Favourite_Tapped"></ToggleButton>
<TextBlock Grid.Column="5" HorizontalAlignment="Left" Margin="-15 0 0 0" VerticalAlignment="Center" Text="{Binding LikeCount}" Foreground="DarkGray"/>
</Grid>
</StackPanel>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
And now when I put the template in the app.xaml file I am getting the following compile error
Events cannot be set in the Application class XAML file
This makes sense for me, but how can I do it anyway? Can I pass the different events like a variable or something into the datatemplate?
//UPDATE - SOLUTION WAY2 using the USERCONTROL:
I've made a UserControl out of the code above and implemented it in the ListView.
<ListView Grid.Row="1" Margin="0 0 0 5"
x:Name = "standardBox"
HorizontalAlignment="Center"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ContinuumNavigationTransitionInfo.IsEntranceElement="True">
<ListView.ItemTemplate>
<DataTemplate>
<local:UCGlobal></local:UCGlobal>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel VerticalAlignment="Bottom"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
You could use a few alternatives:
Use the command property of the buttons inside your templates (take care that your DataContext is set correct, to avoid binding errors):
<DataTemplate x:Key="MyTemplate">
<StackPanel>
<Button x:Name="button1" Content="Button1" Command="{Binding Btn1Command}" CommandParameter="{Binding ElementName=button1}"/>
<Button x:Name="button2" Content="Button2" Command="{Binding Btn2Command}" CommandParameter="{Binding ElementName=button2}"/>
<Button x:Name="button3" Content="Button3" Command="{Binding Btn3Command}" CommandParameter="{Binding ElementName=button3}"/>
</StackPanel>
</DataTemplate>
If it´s another type of event that won't call the command you could use InvokeCommandAction which will be handled same as shown in the next example.
Use a trigger like CallMethodAction (same as above. The DataContext is the place where the method will be searched for.):
<DataTemplate x:Key="MyTemplate">
<StackPanel>
<Button x:Name="button1" Content="Button1" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tapped">
<ei:CallMethodAction MethodName="button1_Tapped"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>...
Write a small UserControl as the base for your DataTemplate:
XAML:
<UserControl x:Class="ThreeBtnUserCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DataTemplateIssue"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<Button x:Name="button1" Content="Button" Click="button1_Click"/>
<Button x:Name="button2" Content="Button" Click="button2_Click"/>
<Button x:Name="button3" Content="Button" Click="button3_Click"/>
</StackPanel>
</UserControl>
Code behind:
public partial class ThreeBtnUserCtrl : UserControl
{
public ThreeBtnUserCtrl()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//...some code only controlling view related stuff of your UserCtrl
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//...some code only controlling view related stuff of your UserCtrl
}
private void button3_Click(object sender, RoutedEventArgs e)
{
//...some code only controlling view related stuff of your UserCtrl
}
}
Use it inside your DataTemplate:
<DataTemplate x:Key="MyTemplate">
<Grid>
<local:ThreeBtnUserCtrl/>
</Grid>
</DataTemplate>
You can use a Command and EventTrigger and have it bound to your View Model’s command.
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tapped">
<i:InvokeCommandAction Command="{Binding TappedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Or use a small UserControl insted a DataTemplate:
<UserControl... >
<StackPanel>
<Button x:Name="button1" Content="Button" Click="button1_Click"/>
</StackPanel>
</UserControl>
And use it in your app.xaml:
<DataTemplate x:Key="Template">
<Grid>
<local:myUserControl/>
</Grid>
</DataTemplate>
Use Behaviours in stackpanel
<DataTemplate x:Key="GlobalBox">
<Border Background="#FFFFFFFF" Margin="10 0 0 5" CornerRadius="2 2 15 2">
<Grid Width="380">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Tag="{Binding ID}" Name="ProfileInfo" Orientation="Horizontal" Margin="15 15 15 0">
<Grid Width="360" Margin="0 0 0 10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Height="45" Width="45" CornerRadius="5">
<Border.Background>
<ImageBrush ImageSource="{Binding ImagePath}" Stretch="UniformToFill"/>
</Border.Background>
</Border>
<StackPanel Grid.Column="1" Orientation="Vertical" Margin="0 5 0 0">
<TextBlock Text="{Binding Name}" Foreground="Black" FontSize="18" FontWeight="Bold"></TextBlock>
<TextBlock Text="{Binding Handle}" Foreground="DarkGray" FontSize="12"/>
</StackPanel>
<Image Grid.Column="2" Source="Assets/ActionIcons/logo_blue_32.png" Width="32" VerticalAlignment="Top"></Image>
</Grid>
<interact:Interaction.Behaviors>
<interactcore:EventTriggerBehavior EventName="Tapped" >
<interactcore:InvokeCommandAction Command="{Binding Path=DataContext.Btn1Command}" />
</interactcore:EventTriggerBehavior>
</interact:Interaction.Behaviors>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
Related
Here's a part of my XAML:
<Grid HorizontalAlignment="Stretch"
Margin="10"
MaxHeight="{Binding Path=ActualHeight,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TabControl Grid.Row="0" MinWidth="270" HorizontalAlignment="Stretch">
<TabItem Header="CPU">
<StackPanel Orientation="Vertical" >
<ListBox Name="CPUListBox"
ItemsSource="{Binding CPUCounters, Mode=OneWay}"
SelectionMode="Multiple"
MaxHeight="{Binding RelativeSource={RelativeSource
AncestorType={x:Type Grid}}, Path=ActualHeight}"
BorderThickness="1"
BorderBrush="#FF8B8B8B"
SelectionChanged="CPUListBox_SelectionChanged"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding CounterName, Mode=OneWay}"/>
<Run Text="{Binding InstanceName, Mode=OneWay}"/>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Name="CPUSelectButtonsGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Name="CPUSelectAllButton"
Margin="0,10,0,0"
Click="CPUSelectAllButton_Click">
<TextBlock Text="SELECT ALL"/>
</Button>
<Button Grid.Column="1" Name="CPUUnSelectAllButton"
Margin="0,10,0,0"
Click="CPUUnSelectAllButton_Click">
<TextBlock Text="UNSELECT ALL"/>
</Button>
</Grid>
</StackPanel>
</TabItem>
<TabItem Header="Memory">
<StackPanel Orientation="Vertical">
<ListBox Name="RAMListBox"
ItemsSource="{Binding RAMCounters, Mode=OneWay}"
SelectionMode="Multiple"
BorderThickness="1"
BorderBrush="#FF8B8B8B"
SelectionChanged="RAMListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding CounterName, Mode=OneWay}" />
<Run Text="{Binding InstanceName, Mode=OneWay}" />
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Name="RAMSelectButtonsGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Name="RAMSelectAllButton"
Margin="0,10,0,0"
Click="RAMSelectAllButton_Click" >
<TextBlock Text="SELECT ALL"/>
</Button>
<Button Grid.Column="1" Name="RAMUnSelectAllButton"
Margin="0,10,0,0"
Click="RAMUnSelectAllButton_Click" >
<TextBlock Text="UNSELECT ALL"/>
</Button>
</Grid>
</StackPanel>
</TabItem>
</TabControl>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
HorizontalAlignment="Right"
Margin="0,15,6,0">
<TextBlock Name="NumberOfSelectionsTextBlock" Text ="0"/>
<TextBlock Text=" items selected"/>
</StackPanel>
</Grid>
Here' what's wrong:
I have a ListBox in each tab of TabControl. When the number of elements in a ListBox is big, the ListBox covers the buttons and I can't get access to them. I tried limitting ListBox's Height to MaxHeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualHeight}", but it's too much - it takes Height of the whole Grid, also covering the Buttons and TextBoxes below them. I'd like the whole thing to be dynamically resized with the Window, so that nothing gets cut off.
I can't find a proper solution to this. What should I do?
The issue here is your stack panel that grows when no of items grow. A better layout would be to use a WPF Grid where you can have two rows.
Second row will contain Buttons and that row can then be given fixed size where as the first row can be set as Width = *
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
</Grid>
I want do some things with my listview on my UWP project:
When a item of the listview is selected I want the entire row selected, also the subitems (cells) on the row should be not selectable.
I was trying with Fullrowselect but seem is not longer availiable in uwp.
my current xaml is:
<ListView x:Name="ListView1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,164,0,0" BorderBrush="Black" BorderThickness="1" Background="WhiteSmoke" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Padding="0" Margin="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBox x:Name="TextBox_Item" Grid.Column="0" Text="{Binding Path=Item}" TextWrapping="Wrap" BorderBrush="Black" BorderThickness="1.5" IsReadOnly="True" TextAlignment="Left"/>
<TextBox x:Name="TextBox_Name" Grid.Column="1" Text="{Binding Path=Name}" TextWrapping="Wrap" BorderThickness="1.5" BorderBrush="Black" IsReadOnly="True" TextAlignment="Left"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Any help is appreciated.
There's a property called "IsHitTestVisible, try changing it to false on your textboxes.
<ListView x:Name="ListView1" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,164,0,0" BorderBrush="Black" BorderThickness="1" Background="WhiteSmoke" Visibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid Padding="0" Margin="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<TextBox x:Name="TextBox_Item" Grid.Column="0" Text="{Binding Path=Item}" TextWrapping="Wrap" BorderBrush="Black" BorderThickness="1.5" IsReadOnly="True" TextAlignment="Left" IsHitTestVisible="False"/>
<TextBox x:Name="TextBox_Name" Grid.Column="1" Text="{Binding Path=Name}" TextWrapping="Wrap" BorderThickness="1.5" BorderBrush="Black" IsReadOnly="True" TextAlignment="Left" IsHitTestVisible="False"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
So I have this XAML as my UserControl. I have an Expander for each property I need to use. In the Header property of the Expander I've done binding to a DataTemplate to use a Image Button. What I need is to edit the fields inside the Expander (put them enabled) when I click the Image Button. But since it is inside of a DataTemplate I don't see the way to ask for it's "major parent" (Expander) and then ask the parent (Expander) to enable the fields.
This is the code of my XAML (the variables are in Spanish)
<UserControl x:Class="Guardian_GUI.Vistas.Recursos.AcordionSupertipos"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="1500">
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid Name="conten" >
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding}" Margin="15 8 15 0" ></TextBlock>
<Button Click="EditarValores" Background="Transparent" Template="{DynamicResource imagenBoton}" Grid.Column="1"/>
</Grid>
</DataTemplate>
<Style TargetType="DockPanel" x:Key="CeldaPropiedades">
<Setter Property="Background" Value="#d08e38"/>
<Setter Property="Margin" Value="5" />
<Setter Property="MinHeight" Value="35" />
</Style>
<Style TargetType="DockPanel" x:Key="CeldaValores">
<Setter Property="Background" Value="#d08e38"/>
<Setter Property="Margin" Value="5" />
<Setter Property="Height" Value="35" />
</Style>
</UserControl.Resources>
<Grid>
<ScrollViewer Name="scroll" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Row="2" Margin="0 0 0 40">
<StackPanel >
<ItemsControl Name="lista" BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ListaSuperTiposEnumerados}" Margin="0" Padding="0" Background="Transparent">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Nombre}" BorderThickness="1" Width="1450" HeaderTemplate="{DynamicResource DataTemplate1}" BorderBrush="#8b8b8b" FontFamily="Myriad Pro" FontStyle="Italic"
FontSize="18" Foreground="#f2f2f2" Background="#333333" VerticalAlignment="Top" Padding="10" >
<ScrollViewer Height="400" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="-10,-10" >
<StackPanel>
<StackPanel Margin="0 20 0 0" >
<DockPanel Grid.Row="0" Grid.ColumnSpan="1" Background="#89673c" Margin="5" MinHeight="35">
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="24" Text="Valores" Grid.Column="0" Grid.Row="0" Margin="0,0,107,0"/>
<CheckBox Content="Editar valores" Name="editadorValores" Margin="0 8 0 0" Foreground="#f2f2f2"/>
</DockPanel>
<Grid Margin="10,0,0,0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="440"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<DockPanel Grid.Column="0" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Id" Grid.Column="0" Grid.Row="1" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="1" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Name="valores" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20"
Text="Nombre" Grid.Column="1" Grid.Row="1" IsEnabled="{Binding ElementName=algod, Path=IsChecked}" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="2" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Descripción" Grid.Column="2" Grid.Row="1" Margin="0,0,107,0"/>
</DockPanel>
<DockPanel Grid.Column="3" Grid.Row="1" Style="{StaticResource CeldaValores}" >
<TextBlock Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="20" Text="Valor" Grid.Column="3" Grid.Row="1" Margin="0,0,0,0"/>
</DockPanel>
</Grid>
<ItemsControl Name="lista_2" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding ValoresSuperTipo}" Margin="0 0 0 15" Padding="0" Background="Transparent"
>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="10,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="140"/>
<ColumnDefinition Width="440"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="160"/>
</Grid.ColumnDefinitions>
<DockPanel Grid.Column="0" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Id}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16"
Text="{Binding Id}" Grid.Column="0" Margin="0,0,0,0" Width="100" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="1" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Nombre}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16"
Text="{Binding Nombre}" Grid.Column="0" Margin="0,0,0,0" Width="400" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="2" Style="{StaticResource CeldaValores}" >
<TextBox ToolTip="{Binding Descripcion}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16" Width="360"
Text="{Binding Descripcion}" Grid.Column="0" Margin="0,0,0,0" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
<DockPanel Grid.Column="3" Style="{StaticResource CeldaValores}">
<TextBox ToolTip="{Binding valor}" Effect="{DynamicResource sombra}" TextWrapping="Wrap" FontSize="16" Width="120"
Text="{Binding valor}" Grid.Column="0" Margin="0,0,0,0" IsEnabled="{Binding IsChecked, ElementName=editadorValores}"/>
</DockPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</ScrollViewer>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
<Button Content="Guardar" Style="{DynamicResource Boton_1}" HorizontalAlignment="Right" Width="120" VerticalAlignment="Bottom" Grid.Row="3" Click="BotonPersistir"/>
<Button Content="Volver" Style="{DynamicResource Boton_1}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="120" Margin="0,0,120,0" Click="BotonVolver" />
</Grid>
</UserControl>
When the event runs I ask for the Parent and TemplatedParent but neither of them are the Expander I need.
private void EditarValores(object sender, RoutedEventArgs e)
{
Button boton = sender as Button;
var Parent = boton.Parent; //It returns a Grid
var TemplatedParent = boton.TemplatedParent; //It returns a ContentPresenter
}
I appreciate your help
from : this blog
you can run this method on your button to get the expander
as:
Expander expander = FindMyParentHelper<Expander>.FindAncestor(boton);
public static class FindMyParentHelper<T> where T : DependencyObject
{
public static T FindAncestor(DependencyObject dependencyObject)
{
var parent = VisualTreeHelper.GetParent(dependencyObject);
if (parent == null) return null;
var parentT = parent as T;
return parentT ?? FindAncestor(parent);
}
}
I have a C# WinRT/8.1 app that uses a ListView with a child stack panel to show items in a horizontal row. That works fine, except I am having the same problem discussed in this SO post:
WinRT Xaml ListView - Touch doesn't scroll well
Except worse. My items don't scroll even when the fingertip is pressed on the margin between items. Unfortunately I don't have a parent Panorama control or ScrollView control to blame. How can I fix this?
NOTE: I switched to a ListView from a GridView because of SO posts I read that indicated GridView's with horizontal items are problematic, which was the case for me.
Here is the XAML for the page:
<Page
<!-- headers snipped for brevity -->
<Page.Resources>
<Converters:DebugBindingConverter x:Key="DebugBindingConverter"/>
<Converters:VideomarkLocationToString x:Key="VideomarkLocationToString"/>
<common:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<DataTemplate x:Key="HorzVideomarksItemTemplate1">
<Grid d:DesignWidth="977" d:DesignHeight="746" Height="121" Width="252">
<Grid.RowDefinitions>
<RowDefinition Height="20*"/>
<RowDefinition Height="52*"/>
<RowDefinition Height="23*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="txtLocation" TextWrapping="Wrap" Text="{Binding OffsetSecs, Converter={StaticResource VideomarkLocationToString}}" Grid.Row="2" />
<TextBlock x:Name="txtNote" Text="{Binding Text}" TextTrimming="CharacterEllipsis"/>
<Image x:Name="imgThumbnail" Grid.Row="1" Source="{Binding ThumbnailAsync}"/>
<!-- <TextBlock x:Name="txtTest2" HorizontalAlignment="Left" Margin="81,93,0,0" TextWrapping="Wrap"
Text="{Binding Videomarks, Converter={StaticResource DebugBindingConverter}}" VerticalAlignment="Top" Height="87" Width="150" FontSize="12"/> -->
</Grid>
</DataTemplate>
</Page.Resources>
<Grid x:Name="gridPage"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="25" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="897*"/>
<ColumnDefinition Width="469*"/>
</Grid.ColumnDefinitions>
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="347*"/>
<RowDefinition Height="231*"/>
</Grid.RowDefinitions>
<!-- Back button and page title -->
<Grid x:Name="gridTopRow" Grid.ColumnSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
Style="{StaticResource NavigationBackButtonNormalStyle}"
VerticalAlignment="Top"
AutomationProperties.Name="Back"
AutomationProperties.AutomationId="BackButton"
AutomationProperties.ItemType="Navigation Button"/>
<TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,894,40" Tapped="pageTitle_Tapped"/>
<Button x:Name="exitButton"
Click="exitButton_Click"
Style="{StaticResource ClosePaneAppBarButtonStyle}" Margin="1065,27,0,9" Grid.Column="1" Width="100" Visibility="{Binding Main.IsDebuggerAttached, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}" />
<Button x:Name="btnTest" Content="test" Grid.Column="1" HorizontalAlignment="Left" Margin="883,59,0,0" VerticalAlignment="Top" FontSize="36" Click="btnTest_Click" Visibility="Collapsed"/>
</Grid>
<WebView x:Name="webViewVideoPlayer" Grid.Row="1" ScriptNotify="ScriptNotifyPlayLocation" Margin="25" />
<Button x:Name="btnVideomark" Content="Bookmark" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="578,53,0,0" Height="54" FontSize="26.667" Click="btnVideomark_Click" Width="181"/>
<ListView
x:Name="listviewVideomarks" Grid.Row="2" Grid.ColumnSpan="2" Opacity="1" IsHitTestVisible="False" Margin="20"
ItemsSource="{Binding Videomarks.VideomarksCollection, Source={StaticResource Locator}}"
ItemTemplate="{StaticResource HorzVideomarksItemTemplate1}" SelectionMode="None"
>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel x:Name="stackVideomarksHorz" Orientation="Horizontal">
</StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<Border x:Name="borderAddVideomark" BorderThickness="5" Grid.ColumnSpan="2" Margin="369,53,319,112" Grid.Row="1" BorderBrush="#FF144989" CornerRadius="25" Opacity="0" IsHitTestVisible="False" Loaded="border_Loaded">
<Grid x:Name="gridAddVideomark" IsHitTestVisible="False" Grid.ColumnSpan="2" Grid.Row="1" Opacity="1" Grid.RowSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="187*"/>
<ColumnDefinition Width="188*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="189*"/>
<RowDefinition Height="137*"/>
<RowDefinition Height="62*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="lblVideomarkLocation" Grid.Column="1" TextWrapping="Wrap" Text="TextBlock" Margin="40,10,10,10" FontSize="18.667"/>
<TextBox x:Name="txtVideomarkNote" Grid.Row="1" TextWrapping="Wrap" Grid.ColumnSpan="2" Margin="10,10,10,2"/>
<Button x:Name="btnOk" Content="OK" HorizontalAlignment="Left" Margin="92,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="192" Foreground="White" FontSize="21.333" Height="45" Click="btnOk_Click"/>
<Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="88,10,0,0" Grid.Row="2" VerticalAlignment="Top" Width="192" Foreground="White" FontSize="21.333" Height="45" Grid.Column="1" Click="btnCancel_Click"/>
<Rectangle x:Name="rectVideomarkThumbnail" Fill="#FFF4F4F5" Stroke="Black" Margin="10"/>
</Grid>
</Border>
</Grid>
</Page>
I am not sure which directions you are trying to scroll, but I think your problem will be fixed if you disable the StackPanel scroll and enable scrolling in both directions on the ListView.
<ListView
x:Name="listviewVideomarks" Grid.Row="2" Grid.ColumnSpan="2" Opacity="1" IsHitTestVisible="False" Margin="20"
ItemsSource="{Binding Videomarks.VideomarksCollection, Source={StaticResource Locator}}"
ItemTemplate="{StaticResource HorzVideomarksItemTemplate1}" SelectionMode="None"
ScrollViewer.HorizontalScrollMode="Auto" ScrollViewer.VerticalScrollMode="Auto"
>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel x:Name="stackVideomarksHorz" Orientation="Horizontal"
ScrollViewer.HorizontalScrollMode="Disabled"
ScrollViewer.VerticalScrollMode="Disabled">
</StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
my problem is that I have only scrollbar for gridview but don't have scrollbar for entire page. Scrollbar should be at the bottom of the page, horizontal. I tried to add scrollbar to the bottom, but it doesn't appear on the page. I've also posted a link to the picture for better understanding. http://i40.tinypic.com/2rm0389.png Sorry that i have only a link, because i need 10 reputation to post images here, it is so bad.
Thanks in advance.
Here is my code (XAML), not entire page, my scrollbar at the bottom:
<!-- Back button and page title -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
<TextBlock x:Name="pageTitle" Text="Профиль" Grid.Column="1" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"/>
</Grid>
<Grid Width="Auto" Margin="0,5,0,0" Grid.Row="1"
>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<Image Source="{Binding ProfileImage}" Stretch="UniformToFill" HorizontalAlignment="Left" Height="270" Margin="165,46,0,0" VerticalAlignment="Top" Width="225"/>
<TextBlock Text="{Binding Name}" HorizontalAlignment="Left" Margin="165,5,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="41" Width="252" FontSize="22" FontWeight="Bold"/>
<GridView x:Name="GridViewToFrinedsPhoto" Margin="461,0,0,0"
ItemsSource="{Binding Mode=TwoWay, Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource MyPhotoItemTemplate}"
SelectionMode="Single"
IsItemClickEnabled="True"
IsSwipeEnabled="false" ItemClick="GridViewToFrinedsPhoto_ItemClick">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
x:Name="ColumnBtn"
AutomationProperties.Name="Group Title" AutomationProperties.LabeledBy="{Binding Title}"
Style="{StaticResource TextPrimaryButtonStyle}" Click="Button_Click_1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
<ScrollBar VerticalAlignment="Bottom" HorizontalAlignment="Center" Width="Auto" RenderTransformOrigin="0.588,-0.824" Visibility="Visible" Margin="0,0,0,603" Orientation="Horizontal" /> -- HERE IS MY SCROLLBAR, DOESN'T WORK :[
</Grid>
</Grid>
My Final code, i hope it helps someone else, I have used scrollviewer and grid container, thanks to Vishal Kaushik from MSDN
<common:LayoutAwarePage
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:VKClient.Views"
xmlns:common="using:VKClient.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vkControls="using:VKClient.VkControls"
xmlns:Controls="using:Callisto.Controls"
x:Name="pageRoot"
x:Class="VKClient.Views.ProfileViewPage"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Mode=Self}}"
mc:Ignorable="d">
<common:LayoutAwarePage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="../Resources/MyStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<CollectionViewSource x:Name="groupedItemsViewSource"
Source="{Binding Items, Mode=TwoWay}"
IsSourceGrouped="True"
ItemsPath="Items"/>
</ResourceDictionary>
</common:LayoutAwarePage.Resources>
<!--
This grid acts as a root panel for the page that defines two rows:
* Row 0 contains the back button and page title
* Row 1 contains the rest of the page layout
-->
<common:LayoutAwarePage.BottomAppBar>
<AppBar x:Name="BottomAppBar2" Padding="10,0,10,0">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Button x:Name="MyProfileButton" Style="{StaticResource MyProfileAppBarButtonStyle}" Click="MyProfileClicked" />
<Button x:Name="MyFriendsButton" Style="{StaticResource MyFriendsAppBarButtonStyle}" Click="MyFriendsClicked"/>
<Button x:Name="MyPhotoButton" Style="{StaticResource MyPhotoAppBarButtonStyle}" Click="MyPhotoClicked" />
<Button x:Name="MyVideosButton" Style="{StaticResource MyVideoesAppBarButtonStyle}" Click="MyVideoesClicked"/>
<Button x:Name="MyAudioButton" Style="{StaticResource MyAudioAppBarButtonStyle}" Click="MyAudioClicked"/>
<Button x:Name="MyGroupsButton" Style="{StaticResource MyGroupsAppBarButtonStyle}" Click="MyGroupsClicked"/>
<!-- <Button x:Name="MyMessagesButton" Style="{StaticResource MyMessagesAppBarButtonStyle}" Click="MyMessagesClicked"/>
<Button x:Name="MyNewsButton" Style="{StaticResource MyNewsAppBarButtonStyle}" Click="MyNewsClicked"
/> -->
</StackPanel>
</Grid>
</AppBar>
</common:LayoutAwarePage.BottomAppBar>
<Grid Style="{StaticResource LayoutRootStyle}" Background="{StaticResource AccentBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Horizontal scrolling grid used in most view states -->
<!-- Vertical scrolling list only used when snapped -->
<!-- Back button and page title -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
<TextBlock x:Name="pageTitle" Text="Профиль" Grid.Column="1" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"/>
</Grid>
<Grid Width="Auto" Margin="0,5,0,0" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<ScrollViewer HorizontalScrollMode="Auto" HorizontalScrollBarVisibility="Auto"
VerticalScrollMode="Disabled" VerticalScrollBarVisibility="Disabled">
<Grid Width="Auto">
<Controls:LiveTile HorizontalAlignment="Left" Margin="20,101,0,0" VerticalAlignment="Top" Width="109" Height="137" Background="LightGreen">
<Controls:LiveTile.ItemTemplate>
<DataTemplate>
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Style="{StaticResource MyGroupsAppBarButtonStyle}" Margin="0,0,0,41" Grid.RowSpan="2"/>
<HyperlinkButton
HorizontalAlignment="Right"
Foreground="LightGray"
Click="GroupClicked" Height="129" Grid.RowSpan="2" Width="90" />
</Grid>
</DataTemplate>
</Controls:LiveTile.ItemTemplate>
</Controls:LiveTile>
<Controls:LiveTile HorizontalAlignment="Left" Margin="20,273,0,0" VerticalAlignment="Top" Width="109" Height="151" Background="Violet">
<Controls:LiveTile.ItemTemplate>
<DataTemplate>
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Style="{StaticResource MyAudioAppBarButtonStyle}" Margin="0,0,0,41" Grid.RowSpan="2"/>
<HyperlinkButton
HorizontalAlignment="Right"
Foreground="LightGray"
FontSize="9" FontWeight="Bold" Click="ProfileAudios" Height="139" Grid.RowSpan="2" Width="99" />
</Grid>
</DataTemplate>
</Controls:LiveTile.ItemTemplate>
</Controls:LiveTile>
<Image Source="{Binding ProfileImage}" Stretch="UniformToFill" HorizontalAlignment="Left" VerticalAlignment="Top" Height="270" Margin="165,46,0,0" Width="225"/>
<TextBlock Text="{Binding Name}" Margin="165,5,0,577" TextWrapping="Wrap" HorizontalAlignment="Left" Height="41" Width="252" FontSize="22" FontWeight="Bold"/>
<GridView x:Name="GridViewToFrinedsPhoto" Margin="461,0,0,0"
ItemsSource="{Binding Mode=TwoWay, Source={StaticResource groupedItemsViewSource}}"
ItemTemplate="{StaticResource MyPhotoItemTemplate}"
SelectionMode="Single"
IsItemClickEnabled="True"
IsSwipeEnabled="false" ItemClick="GridViewToFrinedsPhoto_ItemClick">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
x:Name="ColumnBtn"
AutomationProperties.Name="Group Title" AutomationProperties.LabeledBy="{Binding Title}"
Style="{StaticResource TextPrimaryButtonStyle}" Click="Button_Click_1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
</StackPanel>
</Button>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
</Grid>
</ScrollViewer>
</Grid>
</Grid>