WPF Multilevel TreeView Expanding Issue - c#

I have a multilevel TreeView upto 4 levels as below :
<HierarchicalDataTemplate ItemsSource="{Binding TestModuleStatementList}" x:Key="level4" ItemTemplate="{StaticResource level5}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="600"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="1,1,0,1" Name="lblStatementName" Style="{StaticResource TreeLabelStyle}" MouseLeftButtonUp="ViewTestDataTree_MouseLeftButtonUp" MouseRightButtonUp="ViewTestDataTree_MouseRightButtonUp" Padding="2,2,2,2" Text="{Binding DisplayText}" />
<Border Grid.Column="1" Margin="0,0,-2,0" Style="{StaticResource BorderStylePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockPassCountStyleTestData}"/>
</Border>
<Border Grid.Column="2" Margin="0,0,-2,0" Style="{StaticResource BorderStyleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockFailCountStyleTestData}"/>
</Border>
<Border Grid.Column="3" Margin="0,0,-2,0" Style="{StaticResource BorderStyleModulePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockModulePassCountStyleTestData}"/>
</Border>
<Border Grid.Column="4" Margin="0,0,-2,0" Style="{StaticResource BorderStyleModuleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockModuleFailCountStyleTestData}"/>
</Border>
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding TestModules}" x:Key="level3" ItemTemplate="{StaticResource level4}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="620"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="1,1,0,1" Name="lblCategoryName" Style="{StaticResource TreeLabelStyle}" MouseLeftButtonUp="ViewTestDataTree_MouseLeftButtonUp" MouseRightButtonUp="ViewTestDataTree_MouseRightButtonUp" Padding="2,2,2,2" Text="{Binding DisplayText}" />
<Border Grid.Column="1" Margin="0,0,-2,0" Style="{StaticResource BorderStylePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockPassCountStyleTestData}"/>
</Border>
<Border Grid.Column="2" Margin="0,0,-2,0" Style="{StaticResource BorderStyleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockFailCountStyleTestData}"/>
</Border>
<Border Grid.Column="3" Margin="0,0,-2,0" Style="{StaticResource BorderStyleModulePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockModulePassCountStyleTestData}"/>
</Border>
<Border Grid.Column="4" Margin="0,0,-2,0" Style="{StaticResource BorderStyleModuleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockModuleFailCountStyleTestData}"/>
</Border>
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding TestModuleCategories}" x:Key="level2" ItemTemplate="{StaticResource level3}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="640"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="1,1,1,1" Name="lblDeviceName" MouseLeftButtonUp="ViewTestDataTree_MouseLeftButtonUp" MouseRightButtonUp="ViewTestDataTree_MouseRightButtonUp" ToolTip="{Binding DisplayText}" Style="{StaticResource TreeLabelStyle}" Padding="2,2,2,2" Text="{Binding DisplayText}" />
<TextBox Grid.Column="0" Margin="1,1,1,1" Name="DeviceNameTextBox" LostFocus="TextBox_LostFocus"
Text="{Binding RenameDeviceText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PreviewKeyDown="TextBox_PreviewKeyDown"
Padding="2,2,2,2" MaxLength="100"
Visibility="{Binding Path=Edit, Converter={StaticResource BoolToVisibilityCollapsedConverter}, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
<TextBox.CommandBindings>
<CommandBinding Command="Paste" CanExecute="CommandBinding_CanExecute"/>
</TextBox.CommandBindings>
</TextBox>
<Border Grid.Column="1" Margin="0,0,-1,0" Style="{StaticResource BorderStylePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockPassCountStyleTestData}"/>
</Border>
<Border Grid.Column="2" Margin="0,0,-1,0" Style="{StaticResource BorderStyleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockFailCountStyleTestData}"/>
</Border>
<Border Grid.Column="3" Margin="0,0,-1,0" Style="{StaticResource BorderStyleModulePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockModulePassCountStyleTestData}"/>
</Border>
<Border Grid.Column="4" Margin="0,0,-1,0" Style="{StaticResource BorderStyleModuleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockModuleFailCountStyleTestData}"/>
</Border>
</Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding TestRuns}" x:Key="level1" ItemTemplate="{StaticResource level2}" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="660"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
<ColumnDefinition Width="130"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="1,1,2,1" Name="lblProjectName" MouseLeftButtonUp="ViewTestDataTree_MouseLeftButtonUp" MouseRightButtonUp="ViewTestDataTree_MouseRightButtonUp" Style="{StaticResource TreeLabelStyle}" Padding="2,2,2,2" Text="{Binding DisplayText}"/>
<Border Grid.Column="1" Style="{StaticResource BorderStylePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockPassCountStyleTestData}"/>
</Border>
<Border Grid.Column="2" Style="{StaticResource BorderStyleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockFailCountStyleTestData}"/>
</Border>
<Border Grid.Column="3" Style="{StaticResource BorderStyleModulePassCountTestData}">
<TextBlock Style="{StaticResource TextBlockModulePassCountStyleTestData}"/>
</Border>
<Border Grid.Column="4" Style="{StaticResource BorderStyleModuleFailCountTestData}">
<TextBlock Style="{StaticResource TextBlockModuleFailCountStyleTestData}"/>
</Border>
</Grid>
</HierarchicalDataTemplate>
<TreeView BorderThickness="0" Name="ViewTestDataTree" VirtualizingStackPanel.IsVirtualizing = "True"
VirtualizingStackPanel.VirtualizationMode = "Recycling" ItemsSource="{Binding Projects}" Grid.Row="1"
ItemTemplate="{StaticResource level1}" Background="{StaticResource ATAM_WindowBackground}" FocusVisualStyle="{x:Null}">
<TreeView.ItemContainerStyle>
<!-- Make the focus highlight rectangle, around the current tree item, barely visible -->
<Style TargetType="{x:Type TreeViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF202020" />
<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF202020" />
</Style.Resources>
<Setter Property="IsExpanded" Value="{Binding IsExpanded}"/>
<EventSetter Event="TreeViewItem.Expanded" Handler="TreeViewItem_Expanded"/>
<EventSetter Event="TreeViewItem.Collapsed" Handler="TreeViewItem_Collapsed"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
Here I had a setter in TreeView.ItemContainerStyle that Setter Property="IsExpanded" Value="{Binding IsExpanded}" but when i set TestStamentList which is level 4 IsExpanded property to True from code then that item is not getting expanded. I want to Expand children with some specific condition on a Button Click(Find Next Button) event.
Note:-I have IsExpanded property on all list levels(4 levels) and i am setting IsExpanded=true of last level list(TestModuleStatementList).
Below is my Button Click Event:
private void FindNextFailButton_Click(object sender, RoutedEventArgs e)
{
foreach (var Project in Projects)
{
foreach (var Devices in Project.TestRuns)
{
foreach (var Category in Devices.TestModuleCategories)
{
foreach (var TestModule in Category.TestModules)
{
foreach (var statement in TestModule.TestModuleStatementList)
{
if(statement.ModuleFailed==1)
{
//TreeViewItem item = ViewTestDataTree.ItemContainerGenerator.ContainerFromItem(statement) as TreeViewItem;
//item.IsExpanded = true;
Project.IsExpanded = true;
Devices.IsExpanded = true;
Category.IsExpanded = true;
TestModule.IsExpanded = true;
statement.IsExpanded = true;
statement.IsSelected = true;
return;
//ViewTestDataTree.SelectedItem = statement;
}
}
}
}
}
}
}
}
Kindly provide some help
Thanks In Advance.

I am posting this as answer because i cannot mark solved in comment. https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-find-a-treeviewitem-in-a-treeview

Related

Get current row number of a dynamically generated button in a TreeView HierarchicalDataTemplate

Below Is my Code
I tried the method below, but for every button I get the same row number: 0, I think that is because the first button is default placed at 0th row.
Button btn = sender as Button;
if (btn != null) {
row = Grid.GetRow(btn); // And you have the row number...
}
<TreeViewItem Header="Group by:">
<TreeView Name="TestTreeView" HorizontalAlignment="Stretch" BorderBrush="Transparent"
BorderThickness="0"
ScrollViewer.VerticalScrollBarVisibility="Auto" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type mdl:TaggedCheckBox}"
x:Name="Dispterrs1" ItemsSource="{Binding OrderOfGroup}">
<StackPanel Orientation="Horizontal" x:Uid="TreeStackPanel" x:Name="TreeStackPanel" >
<Grid Name="grd_stack">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="20*" />
<ColumnDefinition Width="30*" />
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Button Style="{StaticResource ButtonStyle}" Grid.Column="0" Height="8" Width="15" Margin="10,0,0,0" x:Name="btnUp" Tag="{Binding TagName}" Click="btnUP_Click">
<Image Source="..\images\up_arrow.png" />
</Button>
<Button Style="{StaticResource ButtonStyle}" Grid.Column="1" Height="8" Width="15" Margin="10,0,0,0" x:Name="btnDown" Tag="{Binding TagName}" Click="btnDown_Click">
<Image Source="..\images\down_arrow.png" />
</Button>
<CheckBox Margin="3,3,0,0" x:Name="cb_TechTerrs" Grid.Column="2"
Tag="{Binding TagName}"
IsChecked="{Binding IsChecked}"></CheckBox>
<TextBlock Margin="5,3,0,0" x:Name="tbTechTerrs" Grid.Column="4" Text="{Binding TagName}"></TextBlock>
</Grid>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</TreeViewItem>

ListView in PivotItem not stretch width in Windows Phone 8.1 WinRT

I have a pivot have 2 itempivot. When I debug the app show as below:
After I close debug, open app it show as below:
Why it is show like that?
Below code
enter code here
<Page
x:Class="WindowsPhone_Crawler.Views.CategoryPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WindowsPhone_Crawler.Views"
xmlns:controls="using:WindowsPhone_Crawler.Controls"
xmlns:converters="using:WindowsPhone_Crawler.Converter"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid x:Name="LayoutRoot" Background="#ebebeb">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Pivot Grid.Row="1"
Margin="-20,-35,-20,0"
x:Name="MyPvNews"
SelectionChanged="MyPvNews_SelectionChanged">
<PivotItem Background="#ebebeb" x:Name="PvHotNews">
<Grid Grid.Row="1" Background="#ebebeb">
<ScrollViewer Name="OuterScroll" Loaded="OuterScroll_Loaded" ViewChanged="OuterScroll_ViewChanged">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="#ebebeb">
<Image Source="/Assets/down13.png" Height="40" Width="40" HorizontalAlignment="Left" Margin="20,0,0,0">
<Image.RenderTransform>
<RotateTransform x:Name="RefreshIndicatiorRotateTransform" CenterX="20" CenterY="20"/>
</Image.RenderTransform>
</Image>
<TextBlock Name="RefreshIndicatiorTextBlock" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="80,0,0,0" Width="200" Foreground="Black"/>
</Grid>
<ListView x:Name="listViewHotNews" Grid.Row="1" IsItemClickEnabled="True" SelectionChanged="listViewHotNews_SelectionChanged" ItemClick="listViewHotNews_ItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="LightGray" BorderThickness="0,0,0,1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Image Grid.RowSpan="2" Grid.Column="0" Source="{Binding Image, Converter={StaticResource StringToImageConverter}}"/>
<TextBlock Text="{Binding Title}" MaxLines="3" TextTrimming="WordEllipsis" Foreground="Black" TextWrapping="Wrap" Grid.Row="0" Grid.Column="1" FontSize="20" FontWeight="Medium" Margin="15,12,0,0"/>
<StackPanel Orientation="Horizontal" Margin="15,0,0,0" Grid.Row="1" Grid.Column="1">
<TextBlock Foreground="Blue" Text="{Binding Name}" TextWrapping="Wrap" Grid.Row="1" Grid.Column="1" FontSize="15" FontWeight="Medium"/>
<TextBlock Foreground="Gray" Text="{Binding Modified_date, Converter={StaticResource TimesConverter}}" TextWrapping="Wrap" Grid.Row="1" Grid.Column="2" FontSize="15" FontWeight="Medium" Margin="5,0,0,0"/>
</StackPanel>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBlock x:Name="TbNoHotNews" VerticalAlignment="Center" Grid.Row="1" FontSize="22" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Center" Visibility="Collapsed">Không có tin nào !</TextBlock>
</Grid>
</ScrollViewer>
</Grid>
</PivotItem>
<PivotItem Background="#ebebeb" x:Name="PvNews">
<Grid Grid.Row="1" Background="#ebebeb">
<GridView x:Name="gridViewTopic" Grid.Row="1"
SizeChanged="gridViewTopic_SizeChanged"
SelectionChanged="gridViewTopic_SelectionChanged"
ItemClick="gridViewTopic_ItemClick"
IsItemClickEnabled="True"
ScrollViewer.VerticalScrollMode="Enabled"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="2" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Visibility="{Binding Regis, Converter={StaticResource VisibilityConverter}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
</Grid.ColumnDefinitions>
<Grid Margin="10" >
<Grid.Background>
<ImageBrush ImageSource="{Binding IconLink, Converter={StaticResource StringToImageConverter}}" />
</Grid.Background>
<TextBlock Foreground="White" Text="{Binding TopicName}" FontSize="23"></TextBlock>
</Grid>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<TextBlock x:Name="TbNoNews" VerticalAlignment="Center" Grid.Row="1" FontSize="22" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Center" Visibility="Collapsed">Không có tin nào !</TextBlock>
</Grid>
</PivotItem>
</Pivot>
<ProgressRing x:Name="myLoading" Grid.Row="1" Background="White" Foreground="Blue" IsActive="True"></ProgressRing>
<Grid Background="#008FE5" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<ToggleButton Margin="10,20,-30,-10" Background="Transparent" VerticalAlignment="Center" IsChecked="{Binding IsLeftPaneOpen, ElementName=HamburgerMenu, Mode=TwoWay}" Style="{StaticResource HamburgerToggleButton}" />
</Grid>
<Grid Grid.Column="1" Margin="0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Background="Transparent" x:Name="GHotNews" Tapped="GHotNews_Tapped" >
<TextBlock x:Name="TbHotNews" Text="Tin hot" FontSize="22" Foreground="White" FontWeight="Bold" VerticalAlignment="Center"/>
<Line x:Name="LHotNews" X1="0" X2="80" Stroke="#FF8B3D" StrokeThickness="2" VerticalAlignment="Bottom">
</Line>
</Grid>
<Grid Grid.Column="1" Background="Transparent" Margin="30,0,-50,0" x:Name="GNews" Tapped="GNews_Tapped" >
<TextBlock x:Name="TbNews" Text="Chuyên mục" FontSize="22" Foreground="White" FontWeight="Bold" Opacity="0.6" VerticalAlignment="Center"/>
<Line x:Name="LNews" X1="0" X2="125" Stroke="#FF8B3D" StrokeThickness="2" VerticalAlignment="Bottom" Visibility="Collapsed"/>
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
</Grid>
</Page>
Did you add style for ListViewItem?
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
It's fair and for GridViewItem, if you're using GridView

Windows 8 XAML ListView Header not same size

Currently having an issue where the header of my ListView is larger than my ListView Items, so the header doesn't line up properly. I could use a margin on the header as a hack to fix it, but surely there's a proper way to fix this?
<DataTemplate x:Key="HeaderTemplate" >
<Grid Height="36" Background="#99999999" Margin="0,0,5,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Uid="Name" TextWrapping="Wrap" HorizontalAlignment="Left" Text="Project" Grid.Column="0" Style="{StaticResource BodyTextBlockStyle}" />
<TextBlock x:Uid="Qty" TextWrapping="Wrap" HorizontalAlignment="Left" Text="Qty" Grid.Column="1" Style="{StaticResource BodyTextBlockStyle}" />
<TextBlock x:Uid="SubTotal" TextWrapping="Wrap" HorizontalAlignment="Left" Text="Sub Total" Grid.Column="2" Style="{StaticResource BodyTextBlockStyle}" />
<TextBlock x:Uid="Total" TextWrapping="Wrap" HorizontalAlignment="Left" Text="Total" Grid.Column="3" Style="{StaticResource BodyTextBlockStyle}" />
</Grid>
</DataTemplate>
// ...
<ListView x:Name="CartGridView" ItemsSource="{Binding CartItmes}" HeaderTemplate="{StaticResource HeaderTemplate}"
Grid.Row="1" VerticalAlignment="Stretch" Width="auto" ItemContainerStyle="{StaticResource SimpleListViewItemStyle}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="auto" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Style="{StaticResource BodyTextBlockStyle}" HorizontalAlignment="Left"
Text="{Binding Name, Mode=TwoWay}"/>
<TextBlock Grid.Column="1" Style="{StaticResource BodyTextBlockStyle}" HorizontalAlignment="Center"
Text="{Binding Qty, Mode=TwoWay}"/>
<TextBlock Grid.Column="2" Style="{StaticResource BodyTextBlockStyle}" HorizontalAlignment="Left"
Text="{Binding SubTotal, Mode=TwoWay}"/>
<TextBlock Grid.Column="3" Style="{StaticResource BodyTextBlockStyle}" HorizontalAlignment="Center"
Text="{Binding Total, Mode=TwoWay}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Add the following to your ListView definition
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>

Create WPF Hierachical TreeView without Creating the template in Resource

I want to create a TreeView that is hierarchical. But I don't want to create the ItemsTemplate in the < StackPanel.Resources > because that will affect my virtualization. That is to say, I don't want to use ItemsSource="{Binding Source={StaticResource dataItems}} in the TreeView. Here is my XAML. Could someone suggest another way to create a TreeView with an ItemsTemplate so that I can preserve virtualization?
<StackPanel>
<StackPanel.Resources>
<exportImport:TreeViewDashboard x:Key="dataItems"/>
<HierarchicalDataTemplate DataType="{x:Type exportImport:TreeViewDashboard}"
ItemsSource="{Binding Path=Components}">
<Grid Margin="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<CheckBox Name="checkbx" Grid.Column="0" Margin="2"
Tag="{Binding Path=Tag}"
Checked="OnCheck" Unchecked="OnUnCheck"></CheckBox>
<Image Margin="2" Grid.Column="1" Source="{Binding Path=ImageUrl}"
Height="14" Width="16" ></Image>
<TextBlock Margin="2" Grid.Column="2" Text="{Binding Path=Name}"
FontWeight="Bold" />
</Grid>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<Grid Margin="2" >
<Grid.ColumnDefinitions>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
<ColumnDefinition ></ColumnDefinition>
</Grid.ColumnDefinitions>
<CheckBox Name="checkbx" Grid.Column="0" Margin="2"
Tag="{Binding Path=Tag}"
Checked="OnCheck" Unchecked="OnUnCheck"/>
<Image Margin="2" Grid.Column="1"
Source="{Binding Path=ImageUrl}"
Height="14" Width="16" />
<TextBlock Margin="2" Grid.Column="2"
Text="{Binding Path=Name}" FontWeight="Bold" />
</Grid>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</StackPanel.Resources>
<TreeView Name="tvES" BorderThickness="0"
ItemsSource="{Binding Source={StaticResource dataItems}}"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
TreeViewItem.Expanded="item_Expanded"
TreeViewItem.Collapsed="item_Collapsed" >
<TreeView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</TreeView.ItemsPanel>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="False" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
</StackPanel>

Creating multiple tabitems from tabitem template in WPF C# XAML

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

Categories

Resources