How to edit an item in the treeview in wpf - wpf

My TreeView ItemTemplate looks like below.
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Connections}">
<WrapPanel >
<CheckBox VerticalAlignment="Center" Command="{Binding UpdateConnections}" CommandParameter="{Binding}" IsChecked="{Binding Status, Mode=TwoWay}" Focusable="False" Style="{StaticResource ResourceKey=TreeView_CheckBox_Style}"></CheckBox>
<TextBlock Text="{Binding Name}" Style="{StaticResource ResourceKey=treeTextBoxStyle}" />
</WrapPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
I am using MVVM. I would like to edit the TextBox if my tree item starts with "s".
I basically double click my TreeViewItem and I should be able to type in it.
How do i do it ?

Related

How to find datacontext of parent's parent for dynamic control

We have following xaml code to display context menu and fire command when delete menu is clicked.
For the context menu item, I would like to bind command which is present in DataContext of ItemsControl. We tried with RelativeSource={RelativeSource TemplatedParent} but as the tree is created dynamically (using dataContext) it is not able to find DeleteCommand.
<Grid x:Name="MyGrid" >
<ItemsControl ItemsSource="{Binding Path=TreeRoot.Children}">
<ItemsControl.ItemTemplate>
<HierarchicalDataTemplate>
<StackPanel>
<Label Background="Black" Content="{Binding Path=DisplayText}"/>
<TreeView ItemsSource="{Binding Converter={StaticResource myCompositeNodeConverter}}" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:CompositeNode}" ItemsSource="{Binding Path=Children}" >
<TextBlock Text="{Binding Path=DisplayText}" Foreground="Black"></TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:LeafNode}" ItemsSource="{Binding Path=Children}">
<TextBlock Text="{Binding Path=DisplayText}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
<TreeView.ContextMenu>
<ContextMenu >
<MenuItem Header="Delete" Command="{Binding DeleteCommand}" />
</ContextMenu>
</TreeView.ContextMenu>
</TreeView>
</StackPanel>
</HierarchicalDataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
How do we bind DeleteCommand for context menu item to DataContext of ItemsControl?

How to set MouseBinding in ItemsControl

I want to call click command on TextBlock, and i do that like this
<TextBlock Text="New Project">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding Path=NewProject}" MouseAction="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
And that is work, but i want to do the same in ItemsControl, so
<ItemsControl Margin="8" ItemsSource="{Binding Path=Projects}" Grid.Row="1" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" Style="{StaticResource ClickableTextBlock}" >
<TextBlock.InputBindings>
<MouseBinding Command="{Binding Path=OpenTasks}" MouseAction="LeftClick"/>
</TextBlock.InputBindings>
</TextBlock>
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And that doesn't work, command is not called. How to do that?
The implicit DataContext inside ItemTemplate is the current data item. But the OpenTasks is not part of each data item. As you said in your comment, it's part of your viewmodel. So in this case you have to set some explicit source with RelativeSource for the Binding. It helps walk up the visual tree and target the source you want. In this case you need the DataContext of the ItemsControl. The code should be like this:
<MouseBinding Command="{Binding DataContext.OpenTasks,
RelativeSource={RelativeSource AncestorType=ItemsControl}}"
MouseAction="LeftClick"/>

WPF Mouse binding in ListBox

I have this code , and i want to add Mouse Left click bindding .
I've done it like this , but for some reason seams that it have on effect.
<ListBox Grid.Row="5" Grid.Column="2" IsEnabled="{Binding UserGroupUpdatePermission}" ItemsSource="{Binding UserGroupTypeList }" HorizontalAlignment="Left" VerticalAlignment="Center" VerticalContentAlignment="Center" Width="300" Height="auto">
<ListBox.InputBindings>
<MouseBinding Command="{Binding Path = SaveCommand }" Gesture="LeftClick" />
</ListBox.InputBindings>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding Checked, Mode=TwoWay}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
am i doing some thing wrong ? is there any outher way for doing it ?

How to use converters without binding paths?

I have a Combobox whose ItemsSource is an ObservableCollection of int values.
My combobox itemtemplate consists on an image and a textblock which content is given by 2 converters.
How can I set this 2 bindings? The following code does not compile:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding, Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding, Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
You need to remove the , so:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>

WPF TreeView selection over entire HierarchyItem

I have a TreeView bound to an MVVM observable collection.
My item template is composed by an image and a textblock like the following code show:
<HierarchicalDataTemplate x:Key="TreeViewItemTemplate" ItemsSource="{Binding Items, Mode=OneWay, NotifyOnSourceUpdated=True}">
<TreeViewItem>
<TreeViewItem.Header>
<StackPanel Orientation="Horizontal">
<Image
Margin="-20,0,5,0"
Source="{Binding Icon, Converter={StaticResource TreeViewIconConverter}, Mode=OneWay}"
Style="{DynamicResource SmallIcon}"/>
<Label Content="{Binding Label}"/>
</StackPanel>
</TreeViewItem.Header>
</TreeViewItem>
</HierarchicalDataTemplate>
The problem happens when you Click over an item. If the Mouse cursor is over the StackPanel, the selection will not happen.
I have included also a screenshot to make this more clear.
Of course this happens because the StackPanel is now over the selection area.
Is there any workaround?
I found the answer by myself.
When you customize the TreeView using an hierarchical data template then you should not replicate the TreeViewItem.Header template because at runtime WPF will create one for you.
So, in order to have a custom TreeViewItem this code is enough:
<HierarchicalDataTemplate x:Key="TreeViewItemTemplate" ItemsSource="{Binding Items, Mode=OneWay, NotifyOnSourceUpdated=True}">
<StackPanel Orientation="Horizontal">
<Image
Margin="0,0,5,0"
Source="{Binding Icon, Converter={StaticResource TreeViewIconConverter}, Mode=OneWay}"
Style="{DynamicResource SmallIcon}"/>
<Label Content="{Binding Label}"/>
</StackPanel>
</HierarchicalDataTemplate>

Resources