Add ToolTip to DataTemplate in WPF TreeView - c#

I'm trying to add a tooltip to children in a TreeView in WPF. However, it appears even in the XAML I can't get a tooltip to render in the treeview children. Can someone please tell me how to get around this?
<Window x:Class="Client_Invoice_Auditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client_Invoice_Auditor"
xmlns:self="clr-namespace:Client_Invoice_Auditor.CoreClientAR"
mc:Ignorable="d"
Title="Client Invoice Auditor" Height="450" Width="1000">
<Window.Resources>
<self:SPConverter x:Key="SPConverter"/>
<self:ErrorExpandConverter x:Key="ErrorExpandConverter"/>
<self:AssignRowConverter x:Key="AssignRowConverter"/>
<self:IssueIconConverter x:Key="IssueIconConverter"/>
</Window.Resources>
<Grid>
<Grid.ColumnDeitions>
</Grid.ColumnDeitions>
<Grid.RowDeitions>
<RowDeition Height="20*"/>
<RowDeition Height="80*"/>
</Grid.RowDeitions>
<Grid Grid.Row="0" Grid.Column="0">
<StackPanel Orientation="Vertical">
<DockPanel VerticalAlignment="Top" Height="20" Panel.ZIndex="1">
<Menu Name="fileMenu" Width="Auto" DockPanel.Dock="Top">
<MenuItem Header="File">
<MenuItem Header="Open Account File" Click="menuOpenFile_Click"/>
<MenuItem Header="Exit" Click="menuExit_Click"/>
</MenuItem>
<MenuItem Header="Options">
<!--<MenuItem Header="Update" Click="update_Click"/>-->
<MenuItem Header="About" Click="about_Click"/>
</MenuItem>
</Menu>
</DockPanel>
<WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="Auto">
<StackPanel Width="Auto" Orientation="Horizontal" HorizontalAlignment="Center">
<Border BorderBrush="MediumAquamarine" BorderThickness="2">
<Label Name="AccountNumber"/>
</Border>
<Border BorderBrush="MediumAquamarine" BorderThickness="2">
<Label Name="AcctDesc"/>
</Border>
<Border BorderBrush="MediumAquamarine" BorderThickness="2">
<Label Name="Organization"/>
</Border>
</StackPanel>
</WrapPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Label Margin="20,10,0,0" Content="Activity Date Time" />
<Label Margin="60,10,0,0" Content="Beginning Balance" />
<Label Margin="10,10,0,0" Content="Charge Amount" />
<Label Margin="30,10,0,0" Content="Adjustments" />
<Label Margin="40,10,0,0" Content="Payments" />
<Label Margin="60,10,0,0" Content="End Balance" />
<Label Margin="50,10,0,0" Content="Issues" />
</StackPanel>
</StackPanel>
</Grid>
<Grid Grid.Row="1" Grid.Column="0">
<Grid Name="IssuesGrid">
<Grid.ColumnDeitions>
<!--<ColumnDeition Width="80*"/>-->
<!--<ColumnDeition Width="20*"/>-->
</Grid.ColumnDeitions>
<TreeView Name="View">
<!--<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded"
Value="{Binding Class, Converter={StaticResource ErrorExpandConverter}}" />
</Style>-->
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded"
Value="{Binding Dummies, Converter={StaticResource ErrorExpandConverter}}" />
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type self:dailyAccountBalance}" ItemsSource="{Binding Dummies}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" IsEnabled="False" Grid.Row="{Binding RowIndex}" ToolTip="This info">
<TextBlock Width="150" Text="{Binding ActivityDate}" />
<TextBlock Width="100" Margin="20,0,0,0" Text="{Binding BegBalance}"/>
<TextBlock Width="100" Margin="20,0,0,0" Text="{Binding ChrgAmount}" />
<TextBlock Width="100" Margin="20,0,0,0" Text="{Binding AdjAmount}" />
<TextBlock Width="100" Margin="20,0,0,0" Text="{Binding PmtAmount}" />
<TextBlock Width="100" Margin="20,0,0,0" Text="{Binding EndBalance}" ToolTipService.IsEnabled="True" ToolTipService.ToolTip="This balance thing" ToolTip="This balance"/>
<ContentControl HorizontalAlignment="Left" Margin="20,0,0,0" Width="100" ToolTip="This">
<ContentControl.Content>
<MultiBinding Converter="{StaticResource IssueIconConverter}">
<Binding Path="RowIndex"/>
<Binding Path="Errors"/>
</MultiBinding>
</ContentControl.Content>
<!--<TextBlock Text="Test"/>-->
</ContentControl>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type self:DummyItem}">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<!--<TextBlock Text="{Binding Text}" />-->
<DataGrid ItemsSource="{Binding ChargeActivities}" AutoGenerateColumns="False">
<DataGrid.Style>
<Style TargetType="{x:Type DataGrid}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Items.Count,
RelativeSource={RelativeSource Self}}" Value="0">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
<DataGrid.Columns>
<DataGridTextColumn x:Name="ChrgID" Header=" Charge" Binding="{Binding ChargeID}" />
<DataGridTextColumn x:Name="ChrgType" Header="Charge Type" Binding="{Binding ChargeType}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Text" Value="CR">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="ChrgAmt" Header="Amount" Binding="{Binding ChargeAmount}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding ChargeType}" Value="CR">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="" Header="" Binding="{Binding Stuff}" />
<DataGridTextColumn x:Name="Class" Header=" Class" Binding="{Binding Class}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding Class, Converter={StaticResource SPConverter}}"/>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
<!--<TreeViewItem x:Key="Test">
<TextBlock Text="Me gusta"></TextBlock>
</TreeViewItem>-->
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Grid>
</Grid>
</Window>
Any suggestions to get tooltips to render in the treeview children would definitely be appreciated. Thanks a ton in advance.

You don't set the tooltip property of elements inside of the template, you set it on the TreeViewItem itself.
Here is a very simplified example:
<TreeView>
<TreeViewItem Header="Item #1" DataContext="Test1" />
<TreeViewItem Header="Item #2" DataContext="Test2" />
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Setters>
<Setter Property="ToolTip" Value="{Binding}" />
</Style.Setters>
</Style>
</TreeView.Resources>
</TreeView>
Here I had to set the DataContext manually and hardcoded for the example to work since just using the Header property doesn't set it, but in the real world, you are going to be data bound, so it will be set.

Related

List view checkbox not getting updated

I have list view in which I have a checkbox "ALL" when I select/unselect "ALL" ,all below checkboxes under "ALL" get selected/unselected. but if list is long ,unselecting "ALL" is not unselecting all below items. Please find the code and suggest if i am missing some property?
<ListView BorderThickness="0" >
<ListViewItem>
<CheckBox Content="{x:Static resources:Resources.SelectAll}"
Height="20"
Margin="0"
VerticalContentAlignment="Center"
Click="checkBox_SelectAll_Click" >
<CheckBox.Style>
<Style TargetType="CheckBox">
<Setter Property="IsChecked"
Value="{x:Null}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsAllSelected}"
Value="true">
<Setter Property="IsChecked"
Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsNoneSelected}"
Value="true">
<Setter Property="IsChecked"
Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</ListViewItem>
</ListView>
<ListView ItemsSource="{Binding Path=FilterItems}"
MaxHeight="90"
SelectionMode="Multiple"
BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Hidden">
Filter items code .here for comboBox_FilterByBrand I want select/unselect "ALL" to work.
<telerik:RadSplitButton.DropDownContent>
<StackPanel Margin="7"
Width="250"
Orientation="Vertical">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0"
Orientation="Horizontal">
<TextBlock Text="{x:Static resources:Resources.Filter}"
Foreground="DarkGray" />
<TextBlock Text=" ("
Foreground="DarkGray" />
<TextBlock Text="{Binding Path=DisplayingCurrentCount}"
Foreground="DarkGray" />
<TextBlock Text="/"
Foreground="DarkGray" />
<TextBlock Text="{Binding Path=DisplayingTotalCount}"
Foreground="DarkGray" />
<TextBlock Text=")"
Foreground="DarkGray" />
</StackPanel>
<Button Name="button_ClearFilters"
Grid.Column="1"
Padding="0"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Left"
Content="{x:Static resources:Resources.ClearFilters}"
Foreground="DarkGray"
Background="Transparent"
BorderBrush="Transparent"
Style="{StaticResource ClearFilterButtonStyle}"
Command="{Binding ClearFiltersButtonCommand}" />
</Grid>
<Controls:FilterControl x:Name="comboBox_FilterByBrand"
HasSeparator="True"
Header="{x:Static resources:Resources.FilterByBrand}"
Type="{x:Static resources:Constants.Filter_By_Brand}"
FilterItems="{Binding BrandFilterItems, IsAsync=True}"
IsAllSelected="{Binding BrandFilterIsAllSelected, Mode=TwoWay}"
IsNoneSelected="{Binding BrandFilterIsNoneSelected, Mode=TwoWay}"
FilterChanged="{Binding FilterSelectionChangedCommand, IsAsync=True}"/>
</StackPanel>

Outlook style Listview in WPF Listview

I am working on a system that manages documents and I currently implement a text search. The result shall be displayed in a list view similar to:
Each hit shall be displayed in a combined row with some information where the tit was found and a short embedded preview. The columns shall be resizable and the text shall be aligned with the width of the columns.
I have seen some examples how this can be done in Windows Forms but not using WPF. Can anybody point me to an example how to create a list view with sch type of rows.
Greetings
Clemens
I figured out that I can do it with datagrid that I style for my needs.
<Window x:Class="DataGridExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridExample"
mc:Ignorable="d"
Title="Hit List" Height="350" Width="350">
<Window.Resources>
<!-- Hover state brush -->
<SolidColorBrush x:Key="HoverBackgroundBrushKey" Color="#E5F3FB" />
<SolidColorBrush x:Key="HoverBorderBrushKey" Color="#70C0E7" />
<!-- Select (activate) the state of the brush -->
<SolidColorBrush x:Key="SelectedActiveBackgroundBrushKey" Color="#CBE8F6" />
<SolidColorBrush x:Key="SelectedActiveBorderBrushKey" Color="#26A0DA" />
<!-- Select (hover) state brush -->
<SolidColorBrush x:Key="SelectedHoverBackgroundBrushKey" Color="#D1E8FF" />
<SolidColorBrush x:Key="SelectedHoverBorderBrushKey" Color="#66A7E8" />
<!-- Select the (failed) status brush -->
<SolidColorBrush x:Key="SelectedInactiveBackgroundBrushKey" Color="#F7F7F7" />
<SolidColorBrush x:Key="SelectedInactiveBorderBrushKey" Color="#DEDEDE" />
</Window.Resources>
<Grid>
<DataGrid x:Name="HitList" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
SelectionMode="Single" SelectionUnit="FullRow" AutoGenerateColumns="False"
GridLinesVisibility="None"
RowDetailsVisibilityMode="Visible"
RowHeaderWidth="0"
VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ItemsSource="{Binding Path=Hits}">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="BackgroundBorder" Background="Transparent">
<ContentPresenter VerticalAlignment="Center" Margin="4,0,6,0" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource HoverBackgroundBrushKey}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource SelectedActiveBackgroundBrushKey}" />
<Setter Property="BorderBrush" Value="{StaticResource SelectedActiveBackgroundBrushKey}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{StaticResource SelectedInactiveBackgroundBrushKey}" />
</MultiTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Document" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DocumentName, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Field" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Field, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Location" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Location, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Length" MinWidth="50" Width="SizeToCells" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Length, Mode=OneWay}" Margin="0,0,12,0" FontWeight="SemiBold"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Border BorderThickness="2,2,8,2"
Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsPresenter}}, Path=ActualWidth}"
HorizontalAlignment="Left" >
<TextBlock Margin="24,0,0,8" TextWrapping="Wrap" HorizontalAlignment="Left">
<TextBlock.Inlines>
<Run Text="{Binding Path=MatchEnvironment.Head}" FontStyle="Italic" Foreground="DarkGray"/>
<Run Text="{Binding Path=MatchEnvironment.Hit}" Background="Yellow" Foreground="Blue" FontWeight="Bold" FontStyle="Italic"/>
<Run Text="{Binding Path=MatchEnvironment.Tail}" FontStyle="Italic" Foreground="DarkGray"/>
</TextBlock.Inlines>
</TextBlock>
</Border>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>

TargetName not found

I have two stackpanels on in another, the inner one has visibility "collapsed", it has the name "verborgen.
When mouseOver the inner stackpanel needs to change to visibility "visible".
So i use TargetName="verborgen"
However, it always returns to me that "The name "verborgen" is not recognized
<DataTemplate x:Key="WastebinTemplate">
<ContentControl map:MapLayer.Position="{Binding GeoLocation}">
<ContentControl.Content>
<StackPanel>
<TextBlock Background="{StaticResource Blue}" Text="{Binding Barcode}"/>
<StackPanel x:Name="verborgen" Visibility="Collapsed" Background="{StaticResource Orange}">
<Button Name="btnRemove" Content="Remove wastebin" Click="btnRemove_Click">
</Button>
<Label>Adres</Label>
<TextBox Name="txbAdres" Text="{Binding Address}"/>
<Label>Location type</Label>
<ComboBox ItemsSource="{Binding Source={StaticResource LocationTypeInstance}, Path=LocationTypes}"
DisplayMemberPath="Description" SelectedItem="{Binding LocationType}" SelectedValuePath="ID" SelectedValue="{Binding LocationType.ID}" />
<Label>Capaciteit</Label>
<Slider Minimum="0" Maximum="100" TickFrequency="10" Value="{Binding Capacity}"></Slider>
</StackPanel>
</StackPanel>
</ContentControl.Content>
</ContentControl>
<DataTemplate.Resources>
<Style TargetType="StackPanel">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Visibility" Value="Visible" TargetName="verborgen" />
<Setter Property="Panel.ZIndex" Value="9999"/>
</Trigger>
</Style.Triggers>
</Style>
</DataTemplate.Resources>
</DataTemplate>
Found it, the problem was instead of a datatemplate style I just used a datatemplate.trigger
<Window x:Class="Oefening2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:map="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
xmlns:model="clr-namespace:Oefening2.model"
Title="Wastebins in Kortrijk" WindowState="Maximized">
<Window.Resources>
<model:LocationType x:Key="LocationTypeInstance"/>
<DataTemplate x:Key="WastebinTemplate">
<ContentControl map:MapLayer.Position="{Binding GeoLocation}">
<ContentControl.Content>
<StackPanel>
<TextBlock Background="{StaticResource Blue}" Text="{Binding Barcode}"/>
<StackPanel x:Name="verborgen" Visibility="Collapsed" Background="{StaticResource Orange}">
<Button Name="btnRemove" Content="Remove wastebin" Click="btnRemove_Click">
</Button>
<Label>Adres</Label>
<TextBox Name="txbAdres" Text="{Binding Address}"/>
<Label>Location type</Label>
<ComboBox ItemsSource="{Binding Source={StaticResource LocationTypeInstance}, Path=LocationTypes}"
DisplayMemberPath="Description" SelectedItem="{Binding LocationType}" SelectedValuePath="ID" SelectedValue="{Binding LocationType.ID}" />
<Label>Capaciteit</Label>
<Slider Minimum="0" Maximum="100" TickFrequency="10" Value="{Binding Capacity}"></Slider>
</StackPanel>
</StackPanel>
</ContentControl.Content>
</ContentControl>
<DataTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="verborgen" Property="Visibility" Value="Visible"/>
<Setter Property="Panel.ZIndex" Value="9999" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid x:Name="Root">
<map:Map x:Name="myMap" CredentialsProvider="AnlnVG80DKkOfS5KLoUofPfy8t0a6AdtJvynRT_rxvV8MQ6cy6eEhQ6MHPBd5P3c" ZoomLevel="14" Center="50.8333,3.2667" MouseDoubleClick="MapWithPushpins_MouseDoubleClick">
<map:MapItemsControl ItemsSource="{Binding Wastebins}" ItemTemplate="{StaticResource WastebinTemplate}" />
</map:Map>
</Grid>
</Window>

Binding XDocument to TreeView - attributes don't show up

I try to bind XDocument to TreeView control. Everything works fine excepting attributes. #%*!$##^% don't want to show up:D
Please, help me to modify that code to make it work:
<SolidColorBrush x:Key="xmlValueBrush" Color="Blue" />
<SolidColorBrush x:Key="xmAttributeBrush" Color="Red" />
<SolidColorBrush x:Key="xmlTagBrush" Color="DarkMagenta" />
<SolidColorBrush x:Key="xmlMarkBrush" Color="Blue" />
<DataTemplate x:Key="AttributeTemplate">
<StackPanel Orientation="Horizontal"
Margin="3,0,0,0"
HorizontalAlignment="Center">
<TextBlock Text="{Binding Path=Name}"
Foreground="{StaticResource xmAttributeBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="=""
Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="{Binding Path=Value, Mode=TwoWay}"
Foreground="{StaticResource xmlValueBrush}" FontFamily="Consolas" FontSize="8pt" />
<TextBlock Text="""
Foreground="{StaticResource xmlMarkBrush}" FontFamily="Consolas" FontSize="8pt" />
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Focusable="False">
<TextBlock x:Name="tbName" Text="Root" FontFamily="Consolas" FontSize="8pt" />
<ItemsControl
ItemTemplate="{StaticResource AttributeTemplate}" HorizontalAlignment="Center"
ItemsSource="{Binding Path=Attributes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<Binding Path="Elements" />
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Value, Mode=TwoWay}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
My TreeView:
<TreeView x:Name="XmlTree" Grid.Row="1"
ItemsSource="{Binding Path=Root.Elements, UpdateSourceTrigger=PropertyChanged}"
ItemTemplate="{StaticResource NodeTemplate}"
SelectedItemChanged="XmlTree_SelectedItemChanged" />
It's my code behind:
private void BindXmlData(string filePath)
{
_xml = XDocument.Load(filePath);
XmlTree.DataContext = _xml;
}
All the nodes display well, but I can't manage with attributes to make them visible
You may change your DataTriggers Sections from:
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
To:
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name.LocalName}" />
</DataTrigger>
Hope will help, thanks! - Shams
As answer did not work for me, I would like to hint to Ashs Answer (Get XML Attributes in WPF with a treeview).
In the original code given here I found another problem. Values of nodes are not rendered in the tree view.
for this to work I needed to remove the first DataTrigger and add another one. so the part
<HierarchicalDataTemplate.Triggers>
...
</HierarchicalDataTemplate.Triggers>
looks like this:
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tbName" Property="Text" Value="{Binding Path=Name}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=FirstNode.NodeType}" Value="Text">
<Setter TargetName="tbName" Property="Text">
<Setter.Value>
<MultiBinding StringFormat="{}{0} = {1}">
<Binding Path="Name"/>
<Binding Path="FirstNode.Value"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
Now each node containing a Value is rendered as:
Node-Name = Value

WPF StackPanel Layout Question

I'm trying to create a layout similar to this:
alt text http://img20.imageshack.us/img20/3533/stackn.png
Here's the code I have:
<StackPanel TextBlock.FontFamily="Segoe UI" Orientation="Horizontal">
<StackPanel HorizontalAlignment="Stretch" Width="Auto">
<TextBlock Padding="5,0,5,0" FontSize="12" FontWeight="Bold" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" />
<TextBlock Padding="5,0,5,0" FontSize="12" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Id}" />
</StackPanel>
<StackPanel>
<TextBlock Padding="5,0,5,0" FontSize="10" Text="Delete">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Padding="5,0,5,0" FontSize="10" Text="Move">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</StackPanel>
Why don't you use a Grid for this?
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="300" />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<TextBlock Text="{Binding Title}" />
</StackPanel>
<StackPanel Grid.Column="1">
<TextBlock Text="Move" />
</StackPanel>
</Grid>
I think you might be better off with a grid as your parent element. Omitting your styles and what not, here's the XAML for the layout in your drawing.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="50" /> <!-- or some other fixed width -->
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0">
<!-- left hand stackpanel content -->
</StackPanel>
<StackPanel Grid.Column="1">
<!-- right hand StackPanel content -->
</StackPanel>
</Grid>
You don't really want a StackPanel for your red container. I'd go with a DockPanel, dock the rightmost blue panel to the right, and ensure LastChildFill is on to ensure the leftmost blue panel expands to the window width.
Here is the code for what I get from your post:
<DockPanel TextBlock.FontFamily="Segoe UI" LastChildFill="True">
<StackPanel DockPanel.Dock="Right">
<TextBlock Padding="5,0,5,0" FontSize="10" Text="Delete">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Padding="5,0,5,0" FontSize="10" Text="Move">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
<StackPanel HorizontalAlignment="Stretch" Width="Auto">
<TextBlock Padding="5,0,5,0" FontSize="12" FontWeight="Bold" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" />
<TextBlock Padding="5,0,5,0" FontSize="12" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Id}" />
</StackPanel>
</DockPanel>
Hope this helps!!

Categories

Resources