I'm using the following code to display some data in a ListView, which is working fine. What I am wanting to do is show a slightly different template in the list depending on the value of a binding value.
Can anyone give some example code to allow me to do that? I want to show a few more textblocks depending on the value of a binding value called Realtime. In other words if Realtime exists show one template, if it doesn't show another.
Thanks in advance!
<ListView x:Name="list" IsItemClickEnabled="False" Margin="0,300,0,0" SelectionMode="None">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Scheduled}"/>
<StackPanel Grid.Column="1">
<TextBlock TextTrimming="WordEllipsis" Text="{Binding Destination}"/>
<TextBlock TextTrimming="WordEllipsis" Text="{Binding Company}"/>
</StackPanel>
<TextBlock Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Route}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You can create a DataTemplateSelector to get a different template based on each item
https://marcominerva.wordpress.com/2013/02/18/dynamically-choose-datatemplate-in-winrt/
//MyDataTemplateSelector.cs
public class MyDataTemplateSelector : DataTemplateSelector
{
public DataTemplate TextTemplate { get; set; }
public DataTemplate ImageTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item,
DependencyObject container)
{
//Here you can cast 'item' and check the RealTime value
if (item is TextItem)
return TextTemplate;
if (item is ImageItem)
return ImageTemplate;
return base.SelectTemplateCore(item, container);
}
}
//XAML
<Page.Resources>
<DataTemplate x:Key="TextDataTemplate">
<Border BorderBrush="LightGray" BorderThickness="1">
<Grid HorizontalAlignment="Left" Width="400" Height="280">
<StackPanel>
<TextBlock Text="{Binding Name}" Margin="15,0,15,20"
Style="{StaticResource TitleTextStyle}"
TextWrapping="Wrap" TextTrimming="WordEllipsis" MaxHeight="40"/>
<TextBlock Text="{Binding Content}" Margin="15,0,15,0" Height="200"
TextWrapping="Wrap" TextTrimming="WordEllipsis"/>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="ImageDataTemplate">
<Grid HorizontalAlignment="Left" Width="400" Height="280">
<Border Background=
"{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding Url}" Stretch="UniformToFill"/>
</Border>
<StackPanel VerticalAlignment="Bottom"
Background=
"{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding Name}"
Foreground=
"{StaticResource ListViewItemOverlayForegroundThemeBrush}"
Style="{StaticResource TitleTextStyle}"
Height="40" Margin="15,0,15,0"/>
</StackPanel>
</Grid>
</DataTemplate>
<local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
TextTemplate="{StaticResource TextDataTemplate}"
ImageTemplate="{StaticResource ImageDataTemplate}">
</local:MyDataTemplateSelector>
</Page.Resources>
<GridView
x:Name="itemGridView"
Padding="116,137,40,46"
ItemTemplateSelector="{StaticResource MyDataTemplateSelector}" />
Related
Here is my code:
<ListView Grid.Row="1" x:Name="viewTicket" Style="{StaticResource ticketListBox}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" BorderBrush="{x:Null}" SelectionChanged="ViewTicket_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Image Visibility="{Binding selectedCheck}" Name="check" Grid.Column="0" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Source="../../Images/check-donatota.png" Stretch="None" MouseLeftButtonUp="Check_MouseLeftButtonUp"/>
<TextBlock Visibility="{Binding selectedQuantity}" Name="quantity" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding amount}"/>
<TextBlock Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" TextWrapping="Wrap" Text="{Binding name}"/>
<TextBlock Visibility="{Binding selectedPrice}" Name="price" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding total, StringFormat=C}"/>
<Image Visibility="{Binding selectedTrash}" Name="trash" Grid.Column="2" Margin="0,0,15,0" HorizontalAlignment="Right" VerticalAlignment="Center" Source="../../Images/trash-donatota.png" Stretch="None" MouseLeftButtonUp="Trash_MouseLeftButtonUp"/>
</Grid>
<ListView
ItemsSource="{Binding ingredients}"
Grid.Row="1"
Margin="-5,0,0,0"
Name="viewTicketIngs"
IsHitTestVisible="False"
Style="{StaticResource ticketListBox}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
Background="Transparent"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Center"
BorderBrush="{x:Null}"
SelectionChanged="ViewTicketIngs_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TextBlock Visibility="Visible" Name="quantity" Grid.Column="0" Foreground="{DynamicResource GrayTextDonaTotaBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding amount}"/>
<TextBlock Margin="10,0,0,0" Grid.Column="1" Foreground="{DynamicResource GrayTextDonaTotaBrush}" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding ing.name}"/>
<TextBlock Visibility="Visible" Name="price" Grid.Column="2" Foreground="{DynamicResource GrayTextDonaTotaBrush}" HorizontalAlignment="Right" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding total, StringFormat=C}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I add data to the ListView viewTicket, but depending of a property I would like to change the ItemSource Binding of the ListView viewTicketIngs. In other words, is there anyway that I can use an if expression on the binding? Something like ItemsSource="{Binding IF(mode == 0) {ingredients} else {plates}}"
Change the Binding by a DataTrigger in a Style:
<ListView ...>
<ListView.Style>
<Style TargetType="ListView">
<Setter Property="ItemsSource" Value="{Binding plates}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding mode}" Value="0">
<Setter Property="ItemsSource" Value="{Binding ingredients}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
</ListView>
As I understand it, you sometimes dispaly plates, sometimes ingredients. Now there are triggers conditional display. WPF actually has a pretty wide support.
However what might be better is to have 2 different ViewModel classes and two (how I call them) "type targetting data tempaltes". Say you have these clases:
abstract class ViewModelItem { }
class Plate : ViewModelItem { }
class IngredientsList : ViewModelItem { }
The propety you exposie this in, would be set to ViewModelItem. In realtiy you would assign either a Plate or IngredientsList Instance.
Now you define two DataTemplates. A interesting thing about WPF is that if you do not specify a explicit Template, the Code will go out of it's way to try to find one. And it will do the matching via the DataType property of the Template (TargetType for Styles and similar). It works similar to what CSS does, with somebodies code going out of it's way to find a template to apply.
I have a StackPanel on a Windows 8.1 app and I want to add a Mouse-Over function.
If I understand correctly the similar function on Windows 8.1 apps is PointerEntered/PointerExited. How can I get the "Item's" text of a TextBlock for example with those functions?
EDIT: I basically want to get the TextBlock's handle when mouse is over it so I can get or set it's properties(text etc...).
This is my C# function
private void itemGridView_PointerEntered(object sender, PointerRoutedEventArgs e)
{
}
and my xaml code:
<GridView
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemGridView"
AutomationProperties.Name="Items In Group"
TabIndex="1"
Grid.RowSpan="2"
Padding="120,126,120,50"
ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick" PointerEntered="itemGridView_PointerEntered">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="110" Width="480" Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110">
<Image x:Name="ImageItemName" Source="{Binding ImagePath}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
</Border>
<StackPanel x:Name="stackPanel1" Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0">
<TextBlock x:Name="ItemTitleText" Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap"/>
<TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
<TextBlock x:Name="ItemDescText" Text="{Binding Description}" Style="{StaticResource BodyTextBlockStyle}" MaxHeight="60"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.Header>
<StackPanel Width="480" Margin="0,4,14,0">
<TextBlock x:Name="TitleTextBlock" Text="{Binding Subtitle}" Margin="0,0,0,20" Style="{StaticResource SubheaderTextBlockStyle}" MaxHeight="60"/>
<Image x:Name="imageName" Source="{Binding ImagePath}" Height="400" Margin="0,0,0,20" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
<TextBlock x:Name="DescTextBlock" Text="{Binding Description}" Margin="0,0,0,0" Style="{StaticResource BodyTextBlockStyle}"/>
</StackPanel>
</GridView.Header>
<GridView.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<Setter Property="Margin" Value="52,0,0,2"/>
</Style>
</GridView.ItemContainerStyle>
</GridView>
Use property Children to getting a UIElementCollection of child elements.
Panel.Children property. MSDN
private void StackPanel_PointerEntered(object sender, PointerRoutedEventArgs e)
{
var values = new List<string>();
var sp = sender as StackPanel;
if (sp != null)
{
foreach (var child in sp.Children)
{
var tb = child as TextBlock;
if (tb != null)
{
values.Add(tb.Text);
}
}
}
}
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 read many tutorials from MSDN about WPF styling and datatemplating and contenttemplating but no success.
I need to make same TabItems in my TabControl and I made manually TabItem which i want to use as host for Style and ContentTemplate for other TabItems in TabControl
<TabItem Header="1.semestar">
<Grid x:Name="GridSemestra">
<Grid.DataContext>
<ViewModel:PredmetVM/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100"/>
<ColumnDefinition MinWidth="30"/>
</Grid.ColumnDefinitions>
<Grid.Children>
<ListBox x:Name="PredmetiLW" Grid.Row="0" BorderThickness="0" Grid.Column="0" ItemsSource="{Binding Predmeti}" HorizontalAlignment="Center" VerticalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Naziv}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="RadioLW" Grid.Row="0" Grid.Column="1" BorderThickness="0" ItemsSource="{Binding Predmeti}" HorizontalAlignment="Center" VerticalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Ocjena}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="Prosjek Semestra :" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" />
<Label x:Name="_prosjekSemestra" Grid.Row="1" Grid.Column="1" ContentStringFormat="F2" Content="{Binding _prosjek, Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="Ostvareni ECTS-ovi :" HorizontalAlignment="right" VerticalAlignment="Center" Grid.Row="2" Grid.Column="0" />
<Label x:Name="_ectsSemestra" Grid.Row="2" Grid.Column="1" Content="{Binding _ectsovi, Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid.Children>
</Grid>
</TabItem>
This is how you define a style for any TabItem. In the example I created a white border and a black background for the Header content of the TabItem:
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border BorderBrush="White" BorderThickness="5" Margin="2">
<Grid Width="100" Height="100" Background="Black">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
RecognizesAccessKey="True"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If you want your items to render with same template set the ItemTemplate for your TabControl like below:
<TabControl ItemsSource="{Binding MyTabItems}">
<TabControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="GridSemestra">
<Grid.DataContext>
<ViewModel:PredmetVM/>
</Grid.DataContext>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100"/>
<ColumnDefinition MinWidth="30"/>
</Grid.ColumnDefinitions>
<Grid.Children>
<ListBox x:Name="PredmetiLW" Grid.Row="0" BorderThickness="0" Grid.Column="0" ItemsSource="{Binding Predmeti}" HorizontalAlignment="Center" VerticalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Naziv}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox x:Name="RadioLW" Grid.Row="0" Grid.Column="1" BorderThickness="0" ItemsSource="{Binding Predmeti}" HorizontalAlignment="Center" VerticalAlignment="Center" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Ocjena}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="Prosjek Semestra :" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Row="1" Grid.Column="0" />
<Label x:Name="_prosjekSemestra" Grid.Row="1" Grid.Column="1" ContentStringFormat="F2" Content="{Binding _prosjek, Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Label Content="Ostvareni ECTS-ovi :" HorizontalAlignment="right" VerticalAlignment="Center" Grid.Row="2" Grid.Column="0" />
<Label x:Name="_ectsSemestra" Grid.Row="2" Grid.Column="1" Content="{Binding _ectsovi, Mode=OneWay}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid.Children>
</Grid>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
Doing this, for all the items in property MyTabItems, TabItems will be generated
I'm trying to create a listbox template (for the items). This is what I've got.
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Text="TEKS" FontSize="20" FontWeight="Bold" Background="#FF502F8F" Foreground="White" Width="{Binding Path=Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}}" TextAlignment="Center" />
<TextBlock Text="{Binding Level}" FontSize="24" Background="#FF058C44" HorizontalAlignment="Center" Width="{Binding Path=Width, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}}" Foreground="White" TextAlignment="Center" FontFamily="Segoe UI Light" />
</StackPanel>
<TextBlock Grid.Column="1" Text="{Binding Owner}" FontSize="20" FontWeight="Bold" TextWrapping="Wrap" />
</Grid>
</DataTemplate>
</Window.Resources>
<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<ListBox ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" Margin="44,39,82,103" Style="{DynamicResource ListBoxStyle}"/>
</Grid>
And this is image how it looks.
Can you see the textblock extends more the width? I want to show the string wrap inside the listbox. What am I doing wrong?
Disable horizontal scrolling:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...>
Enable text wrapping (already the case):
<TextBlock TextWrapping="Wrap" ...>