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
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>
It should be looks like such as on the image
I have an list view with custom DataTemplate. I have to autofill all free space with ellipsis in Description TextBlock.
Do you have any idea how i can do this?
<ListView ItemsSource="{Binding Items}" HorizontalAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Width="16" Height="16" Source="{Binding Type, Converter={StaticResource NodeIconConverter}}" />
<TextBlock Grid.Column="1" Text="{Binding Tittle}" ToolTip="{Binding Description}" />
<Image Grid.Column="2" Width="16" Height="16" Source="{Binding Status, Converter={StaticResource NodeStatusConverter}}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You can update your DataTemplate like below to have dotted line between space:
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Width="16" Height="16" Source="{Binding Type, Converter={StaticResource NodeIconConverter}}" />
<TextBlock Grid.Column="1" Text="{Binding Tittle}" ToolTip="{Binding Description}" />
<Rectangle Grid.Column="2" Margin="2,5,2,0" Stroke="#FF000000" Height="1" StrokeThickness="1" StrokeDashArray="1 4"
SnapsToDevicePixels="True"/>
<Image Grid.Column="3" Width="16" Height="16" Source="{Binding Status, Converter={StaticResource NodeStatusConverter}}" />
</Grid>
</DataTemplate>
<Grid Name="WeightGrid" Grid.RowSpan="2" SnapsToDevicePixels="True" Grid.ColumnSpan="2" Margin="{Binding WeigntGridMargin}" MouseDown="WeigntGridWrap_OnMouseDown" MouseMove="WeigntGridWrap_OnMouseMove" MouseUp="WeigntGridWrap_OnMouseUp">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
...
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<TranslateTransform x:Name="weightTT"/>
</Grid.RenderTransform>
<StackPanel Grid.Column="0" Width="100" Height="75">
<StackPanel.Background>
<ImageBrush ImageSource="/Size.WPF;component/Assets/ruller-bg.png" />
</StackPanel.Background>
<TextBlock Margin="0, 42, 0, 0" Foreground="#c0b6d1" HorizontalAlignment="Center" FontSize="18">0</TextBlock>
</StackPanel>
<StackPanel Grid.Column="1" Width="100" Height="75">
<StackPanel.Background>
<ImageBrush ImageSource="/Size.WPF;component/Assets/ruller-bg.png" />
</StackPanel.Background>
<TextBlock Margin="0, 42, 0, 0" Foreground="#c0b6d1" HorizontalAlignment="Center" FontSize="18">1</TextBlock>
</StackPanel>
<StackPanel Grid.Column="2" Width="100" Height="75">
<StackPanel.Background>
<ImageBrush ImageSource="/Size.WPF;component/Assets/ruller-bg.png" />
</StackPanel.Background>
<TextBlock Margin="0, 42, 0, 0" Foreground="#c0b6d1" HorizontalAlignment="Center" FontSize="18">2</TextBlock>
</StackPanel>
...
</Grid>
I have Grid with many columns. Every column is StackPanel with different column number and with different number in TextBlock. To decrease amount of code I want to use ItemsContol or something like this to build grid columns. But one problem - How can I bind ColumnNumber to ItemsControl Item? Possibly there is solution Bind Grid.Row / Grid.Column inside a DataTemplate But another problem - How can set amount of Columns?
UPD :
<ItemsControl Name="WeightItemsControl" ItemsSource="{Binding Cells}" Grid.RowSpan="2" Grid.ColumnSpan="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid Name="WeightGrid" SnapsToDevicePixels="True" Margin="{Binding WeigntGridMargin}" MouseDown="WeigntGridWrap_OnMouseDown" MouseMove="WeigntGridWrap_OnMouseMove" MouseUp="WeigntGridWrap_OnMouseUp">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding GridRow}" />
<Setter Property="Grid.Column" Value="{Binding GridColumn}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
This is what I have now. But I still don't know how to set Amount of columns that should be created to it ItemsControl (Currently it's just binded to some collection Cells but it should be just number as amount of columns).
<ItemsControl Name="icTodoList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" />
<ProgressBar Grid.Column="1" Minimum="0" Maximum="100" Value="{Binding Completion}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
you can modify the code as you want..or if you don't use the binding you can simply do like this
<ItemsControl>
<system:String>ItemsControl Item #1</system:String>
<system:String>ItemsControl Item #2</system:String>
<system:String>ItemsControl Item #3</system:String>
<system:String>ItemsControl Item #4</system:String>
<system:String>ItemsControl Item #5</system:String>
</ItemsControl>
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>