HierarchicalDataTemplate binding - wpf

I have an hierarchical Treeview which each item contains a complexed entity which consists of:
Border
-- Check box
-- TextBlock
i want to change the opacity of the border whenever the checkbox IsEnabled property is set to "False"
this is what i have:
<HierarchicalDataTemplate DataType="{x:Type sd:LegendTreeViewItem}" ItemsSource="{Binding Path=SubSensors}">
<Border x:Name="treeViewItemBorder" Height="24" Margin="3 3 3 3" Width="350" Background="{Binding Path=Color}" CornerRadius="8 8 8 8">
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="treeViewItemCheckbox" Margin="5 5 5 5" Click="CheckBox_Click" IsChecked="{Binding IsChecked}" VerticalAlignment="Center" IsEnabled="{Binding Enabled}" Style="{StaticResource OpacityOnDisabled}"/>
<TextBlock Height="Auto" FontFamily="Tahoma" FontWeight="Bold" Foreground="Black" HorizontalAlignment="Left" Text="{Binding Path=Name}"
VerticalAlignment="Center" ToolTip="{Binding Path=Name}"/>
</StackPanel> </Border>
</HierarchicalDataTemplate>
<TreeView x:Name="legendTypeTree" Grid.Row="1" Foreground="White" ItemsSource="{Binding ElementName=uc, Path=TypeItemsSource}">
<TreeView.Resources>
<Style x:Key="OpacityOnDisabled" TargetType="{Binding RelativeSource={RelativeSource AncestorType=Border}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="False">
<Setter Property="Border.Opacity" Value="0.3"/>
<Setter Property="Border.Background" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TreeView.Resources>
</TreeView>

Move both HierarchicalDataTemplate and Style for the Border in the TreeView resources. (Style first in order for the StaticResource OpacityOnDisabled to resolve properly).
You want to change the style of the border, so there is no sense to apply style on the checkbox. Move Style="{StaticResource OpacityOnDisabled}" from the CheckBox to the Border element. Set the correct type of the style and for the DataTrigger change the bindinig to Binding="{Binding ElementName=treeViewItemCheckbox, Path=IsChecked}"
In the end you should have something like, which should do what you want:
Result:
<TreeView
x:Name="legendTypeTree"
Grid.Row="1"
Foreground="White"
ItemsSource="{Binding ElementName=uc, Path=TypeItemsSource}">
<TreeView.Resources>
<Style x:Key="OpacityOnDisabled" TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=treeViewItemCheckbox, Path=IsChecked}" Value="False">
<Setter Property="Border.Opacity" Value="0.3"/>
<Setter Property="Border.Background" Value="White"/>
</DataTrigger>
</Style.Triggers>
</Style>
<HierarchicalDataTemplate DataType="{x:Type sd:LegendTreeViewItem}" ItemsSource="{Binding Path=SubSensors}">
<Border
x:Name="treeViewItemBorder"
Height="24"
Margin="3 3 3 3"
Width="350"
Background="{Binding Path=Color}"
CornerRadius="8 8 8 8"
Style="{StaticResource OpacityOnDisabled}">
<StackPanel Orientation="Horizontal">
<CheckBox
x:Name="treeViewItemCheckbox"
Margin="5 5 5 5"
Click="CheckBox_Click"
IsChecked="{Binding IsChecked}"
VerticalAlignment="Center"
IsEnabled="{Binding Enabled}" />
<TextBlock
Height="Auto"
FontFamily="Tahoma"
FontWeight="Bold"
Foreground="Black"
HorizontalAlignment="Left"
Text="{Binding Path=Name}"
VerticalAlignment="Center"
ToolTip="{Binding Path=Name}"/>
</StackPanel>
</Border>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>

Related

WPF: Grid inside DataGridTemplateColumn not expanding

I'm facing two problems with my projet:
SpackPanel not expanding the space available inside DataGridTemplateColumn
TextBlock not wrapping
I have a DataGrid with three different columns, and I want the one in the middle to use all available space left. Also in that column I have a StackPanel because I need to have two elements inside:
Border with icon inside that just appears depending on Use_Half_Portion value
TextBlock showing text
Giving background colors to the elements I have noticed that the StackPanel doesn't fill all of the available space left insite the column, and the wrap in the TextBlock is not being respected. Im supposing that the StackPanel is not assuming the MaxWidth of the DataGridTemplateColumn and it's using unlimited space, and because of that the TextBlock assumes that the limit is not being reached to make the text wrap.
Here's my code:
<DataGrid x:Name="ConsumptionList" ItemsSource="{Binding ConsumptionList}"
AutoGenerateColumns="False" IsReadOnly="True"
CanUserReorderColumns="False" CanUserResizeColumns="False"
BorderBrush="{x:Null}" VerticalContentAlignment="Center" CanUserSortColumns="False" HeadersVisibility="Column" Margin="0,0,0,-4" Background="{x:Null}">
<DataGrid.Resources>
<Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource foScrollBar.Small}"/>
</DataGrid.Resources>
<DataGrid.ColumnHeaderStyle>
<StaticResource ResourceKey="clientScreenList.Header"/>
</DataGrid.ColumnHeaderStyle>
<DataGrid.RowStyle>
<StaticResource ResourceKey="clientScreenList.Lines"/>
</DataGrid.RowStyle>
<DataGrid.CellStyle>
<StaticResource ResourceKey="clientScreenList.Cells"/>
</DataGrid.CellStyle>
<DataGrid.Style>
<StaticResource ResourceKey="foBillingList"/>
</DataGrid.Style>
<DataGrid.Columns>
<!--quantity-->
<DataGridTextColumn x:Name="ConsQttColumn" MinWidth="20" Width="Auto" MaxWidth="80" FontFamily="Segoe UI Semibold"
Binding="{Binding Quantity, Converter={StaticResource DecimalToQuantityConverter}}" Header="{x:Static localization:LanguageRes.consList_quantity}" />
<!--description-->
<DataGridTemplateColumn x:Name="ConsDescriptionColumn" Header="Descrição" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Background="#FFDC1A1A">
<Border CornerRadius="30" Grid.Column="0" Width="30" Height="30" Background="{x:Null}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,3,0">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding Use_Half_Portion}" Value="1">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<TextBlock Grid.Column="1" TextWrapping="Wrap" HorizontalAlignment="Center" Style="{StaticResource icon}" TextAlignment="Center" FontSize="22" Text="{x:Static TechUI:AppConstants+ICONS.HalfDose}" Padding="0"
Margin="0" Foreground="Black" Background="{x:Null}" />
</Border>
<TextBlock Text="{Binding Product_Description}" HorizontalAlignment="Left" TextAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Center" FontFamily="Segoe UI Semibold" Background="#FFB4AD1C" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- total price-->
<DataGridTemplateColumn x:Name="ConsTotalColumn" Header="Preço Total" Width="Auto" MaxWidth="135">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" MaxWidth="135" VerticalAlignment="Center" >
<TextBlock MaxWidth="135" HorizontalAlignment="Stretch" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" FontFamily="Segoe UI Semibold" >
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="{Binding Total_W_Vat, Converter={StaticResource DecimalToPriceConverter}}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Is_Composite}" Value="1">
<Setter Property="Text" Value=" " />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
As you can see in the image, the red element is the StackPanel and the green element is the TextBlock. In the first line you can see that the text is being cut and not being wrapped, the full text is "Almondegas Frango"
I have found a solution.
Just changed the StackPanel for a DockPanel and it worked the exact same way I wanted!

WPF: Single TreeViewItem bold

I am new to Xaml and somehow i got stuck here. I googled a lot of different approaches but neither did work :-( I hope someone has here has an idea and can help me :-)
With the help of a few tutorials in the internet I got this TreeView. It works like a charme :-)
<TreeView>
<TreeViewItem ItemsSource="{Binding Data.Number}">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Width="15" Margin="5 0 5 0" Source="image.ico"/>
<TextBlock Text="{Binding Data.ID}"/>
<TextBlock Text=" [" Foreground="Blue"/>
<TextBlock Text="{Binding Data.Number.Count}" Foreground="Blue"/>
<TextBlock Text="]" Foreground="Blue"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem.ItemTemplate>
<DataTemplate>
<TreeViewItem Header="{Binding ItemID}">
<TreeViewItem ItemsSource="{Binding ItemInfo}">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Width="15" Margin="5 0 5 0" Source="image.ico"/>
<TextBlock Text="Items"/>
<TextBlock Text=" [" Foreground="Blue"/>
<TextBlock Text="{Binding ItemInfo.Count}" Foreground="Blue"/>
<TextBlock Text="]" Foreground="Blue"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value.Number}"/>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
<TreeViewItem ItemsSource="{Binding SetInfo}">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Width="15" Margin="5 0 5 0" Source="image.ico"/>
<TextBlock Text="Sets"/>
<TextBlock Text=" [" Foreground="Blue"/>
<TextBlock Text="{Binding SetInfo.Count}" Foreground="Blue"/>
<TextBlock Text="]" Foreground="Blue"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value.ID}"/>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
<TreeViewItem ItemsSource="{Binding GroupInfo}">
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image Width="15" Margin="5 0 5 0" Source="image.ico"/>
<TextBlock Text="Groups"/>
<TextBlock Text=" [" Foreground="Blue"/>
<TextBlock Text="{Binding GroupInfo.Count}" Foreground="Blue"/>
<TextBlock Text="]" Foreground="Blue"/>
</StackPanel>
</TreeViewItem.Header>
<TreeViewItem.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value.Number}"/>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
</TreeViewItem>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>
</TreeView>
The problem is now that I want the selected item (with mouse click) gets bold. But only the selected item (not items below or above). It doesnt matter on which level the item is.
I tried this a lot of variations but none did work. This two were the best ones but they always select a hole node or just the top node and I couldnt break it down for one single node.
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}, FallbackValue=False}" Value="True">
<Setter Property="TextBlock.FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
Can you try this Style?
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="FontWeight" Value="Normal"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.Resources>

TextBlock in DataGridTemplateColumn.CellTemplate losing Validation Styles when scrolling

I have a data grid built using DataGridTemplateColumns. When a validation error exists, we are displaying it on the Textblock of the CellTemplate. When scrolling occurs, the error styling is lost after it scrolls off the page.
In my control resources:
<ControlTemplate x:Key="validationTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Foreground="Yellow"
FontSize="20"
VerticalAlignment="Center"
Margin="0,0,2,2">!</TextBlock>
<Border Grid.Column="1"
BorderBrush="Yellow"
BorderThickness="1"
Margin="0"
Padding="0"
Height="19">
<AdornedElementPlaceholder/>
</Border>
</Grid>
</ControlTemplate>
<Style x:Key="TextBlockValidationStyle" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
And Within the DataGrid:
<DataGridTemplateColumn Header="Destination Column">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox IsEditable="True"
IsTextSearchCaseSensitive="{Binding ElementName=caseSensitiveSearch, Path=IsChecked}"
ItemsSource="{Binding AllSuggestedNames}"
TextSearch.TextPath="SuggestedName"
Text="{Binding ColumnMapping.DestinationColumnName, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Style="{StaticResource TextBlockValidationStyle}"
Text="{Binding ColumnMapping.DestinationColumnName,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Solved it! Wrapping each CellTemplate's interior of their DataTemplate with an AdornerDecorator fixed it right up!
<DataTemplate>
<AdornerDecorator>
<TextBlock Style="{StaticResource TextBlockValidationStyle}"
Text="{Binding ColumnMapping.DestinationColumnName,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{StaticResource validationTemplate}"/>
</AdornerDecorator>
</DataTemplate>

Force binding update in XAML

I have a View with a Listbox and several textboxes bound to properties of the objects displayed in the listbox. On opening, the listbox is populated with data, and I have the following in the style to ensure than when there are items and nothing is selected, to select the 1st item.
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="SelectedItem" Value="{x:Null}"/>
<Condition Property="HasItems" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="SelectedIndex" Value="0"/>
</MultiTrigger>
</Style.Triggers>
This works. The first item in the list is always selected when the list gets populated.
Unfortunately, even though the first item is selected, the textboxes that are bound to the selectedItems properties (via their parent grids datacontext) do not seem to receive notification.
Anyone know of a way to force them to update (in XAML if possible). Currently, bindings look thusly:
<TextBox Text="{Binding Weight, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />
Edit
Below is the XAML showing the grid PackageDetailsGrid using the PackageList SelectedItem as its datacontext:
<StackPanel Orientation="Vertical" d:LayoutOverrides="Height">
<TextBlock Text="Packages" Style="{DynamicResource TitleText}"/>
<ListBox x:Name="PackageList" Style="{StaticResource SnazzyList}" FocusVisualStyle="{x:Null}" Margin="0" ItemsSource="{Binding Source={StaticResource Packages}}" HorizontalContentAlignment="Stretch" Height="132.5" Background="#18000000">
</ListBox>
<Grid Margin="0,0,8,0">
<Button Content="Add" Margin="20,0,0,0" Width="87" HorizontalAlignment="Left" Style="{DynamicResource ClearButton}" Command="{Binding AddPackageCommand}" Visibility="{Binding ShipmentRecord.TransitStatus, Converter={StaticResource ShippedToVisibilityConverter}}"/>
<Button Content="Delete" Margin="0,0,20,0" Style="{DynamicResource ClearButton}" HorizontalAlignment="Right" Width="87" Height="21.4666666666667" Command="{Binding DeletePackageCommand}" CommandParameter="{Binding SelectedItem, ElementName=PackageList, Mode=Default}" Visibility="{Binding ShipmentRecord.TransitStatus, Converter={StaticResource ShippedToVisibilityConverter}}"/>
</Grid>
</StackPanel>
<Grid x:Name="PackageDetailsGrid" Margin="0" Grid.Column="1" DataContext="{Binding Items.CurrentItem, ElementName=PackageList, Mode=Default}">
<StackPanel Margin="0">
<Grid Margin="0,0,0,8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*"/>
<ColumnDefinition Width="60*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Dimensions" Foreground="White" FontWeight="Bold"/>
<StackPanel Grid.Column="1" Orientation="Horizontal" d:LayoutOverrides="Height">
<TextBox Text="{Binding Height, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="48" TextWrapping="Wrap" Margin="0" HorizontalAlignment="Left" IsEnabled="{Binding CanEnterPackageDetails}">
<i:Interaction.Behaviors>
<local:SelectAllOnFocusTextboxBehavior/>
</i:Interaction.Behaviors>
</TextBox>
<TextBlock Text="X" Style="{DynamicResource XTextBlockStyle}"/>
<TextBox Text="{Binding Width, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="48" TextWrapping="Wrap" Margin="0" HorizontalAlignment="Left" IsEnabled="{Binding CanEnterPackageDetails}">
<i:Interaction.Behaviors>
<local:SelectAllOnFocusTextboxBehavior/>
</i:Interaction.Behaviors>
</TextBox>
<TextBlock Text="X" Style="{DynamicResource XTextBlockStyle}"/>
<TextBox Text="{Binding Length, ConverterParameter=\{0:F\}, Converter={StaticResource FormattingConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Width="48" TextWrapping="Wrap" Margin="0" HorizontalAlignment="Left" IsEnabled="{Binding CanEnterPackageDetails}">
<i:Interaction.Behaviors>
<local:SelectAllOnFocusTextboxBehavior/>
</i:Interaction.Behaviors>
</TextBox>
</StackPanel>
</Grid>
</StackPanel>
</Grid>
Here is some xaml for a TextBox whose Text property is bound to the SelectedItem property of the list view. It includes your code for automatically selecting the first item when there is nothing selected.
Is this the kind of solution you're looking for? If not, I need more info i.e. all of the relevant code you're working on.
<StackPanel>
<TextBox Text="{Binding SelectedItem, ElementName=MyListView}" />
<ListView x:Name="MyListView">
<ListView.Style>
<Style TargetType="ListView">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="SelectedItem" Value="{x:Null}"/>
<Condition Property="HasItems" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="SelectedIndex" Value="0"/>
</MultiTrigger>
</Style.Triggers>
</Style>
</ListView.Style>
<ListView.Items>
<System:String>hello</System:String>
<System:String>world</System:String>
</ListView.Items>
</ListView>
</StackPanel>

Is it possible to template a template in WPF XAML?

Is it possible to use templates within templates?
For instance, I have the following two templates:
<HierarchicalDataTemplate
x:Key="RecursiveTemplate"
ItemsSource="{Binding Children}">
<StackPanel Margin="1" Orientation="Horizontal">
<Ellipse Fill="DarkGreen" Width="14" Height="14"/>
<TextBlock
MouseDown="OnTreeMouseDown"
TargetUpdated="OnTargetUpdated"
Visibility="{Binding Editing, Converter={StaticResource visibilityInverter}}"
Margin="5"
Text="{Binding Name}"/>
<TextBox
PreviewKeyDown="OnTreeKeyDown"
Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}"
Margin="2"
Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate
x:Key="ContainerTemplate"
ItemsSource="{Binding Children}">
<StackPanel Margin="1" Orientation="Horizontal">
<Ellipse Fill="DarkBlue" Width="14" Height="14"/>
<TextBlock
MouseDown="OnTreeMouseDown"
TargetUpdated="OnTargetUpdated"
Visibility="{Binding Editing, Converter={StaticResource visibilityInverter}}"
Margin="5"
Text="{Binding Name}"/>
<TextBox
PreviewKeyDown="OnTreeKeyDown"
Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}"
Margin="2"
Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
There's a section of identical XAML:
<TextBlock
MouseDown="OnTreeMouseDown"
TargetUpdated="OnTargetUpdated"
Visibility="{Binding Editing, Converter={StaticResource visibilityInverter}}"
Margin="5"
Text="{Binding Name}"/>
<TextBox
PreviewKeyDown="OnTreeKeyDown"
Visibility="{Binding Editing, Converter={StaticResource visibilityConverter}}"
Margin="2"
Text="{Binding Name}"/>
Is it possible to move that to a resource and refer to it by name, rather than repeat it?
The answer appears to be "almost"
<Style x:Key="textBlockStyle" TargetType="TextBlock">
<Setter Property="Visibility" Value="{Binding Editing, Converter={StaticResource visibilityInverter}}"/>
<Setter Property="Margin" Value="4"/>
<Setter Property="Text" Value="{Binding Name}"/>
<EventSetter Event="MouseDown" Handler="OnTreeMouseDown"/>
</Style>
<Style x:Key="textBoxStyle" TargetType="TextBox">
<Setter Property="Visibility" Value="{Binding Editing, Converter={StaticResource visibilityConverter}}"/>
<Setter Property="Margin" Value="1"/>
<Setter Property="Text" Value="{Binding Name, NotifyOnSourceUpdated=True}"/>
<EventSetter Event="PreviewKeyDown" Handler="OnTreeKeyDown"/>
</Style>
Then the templates reduce to
<HierarchicalDataTemplate
x:Key="RecursiveTemplate"
ItemsSource="{Binding Children}">
<StackPanel Margin="1" Orientation="Horizontal">
<Ellipse Fill="DarkGreen" Width="14" Height="14"/>
<TextBlock
Style="{StaticResource textBlockStyle}"/>
<TextBox
SourceUpdated="OnSourceUpdated"
Style="{StaticResource textBoxStyle}"/>
</StackPanel>
</HierarchicalDataTemplate>
However, note you cannot put SourceUpdated in a style, as it's not covered by EventSetter

Resources