Can't use attached property on combobox inside hierarchical datatemplate WPF
Posted
by jesse_t_r
on Stack Overflow
See other posts from Stack Overflow
or by jesse_t_r
Published on 2010-05-23T23:10:10Z
Indexed on
2010/05/24
9:11 UTC
Read the original article
Hit count: 385
I'm hoping to use an attached property to assign a command to the selection changed event of a combobox that is embedded inside a treeview. I'm attempting to set the attached property inside the hierchical data template for the tree but the command is not set and does not fire when the item in the combobox is changed.
I've found that setting the attached property directly on a combobox outside of a datatemplate works fine;
here is how I'm trying to set the property in the template:
<HierarchicalDataTemplate x:Key="template1"
ItemsSource="{Binding Path=ChildColumns}">
<Border
Background="{StaticResource TreeItem_Background}"
BorderBrush="Blue"
BorderThickness="2"
CornerRadius="5"
Margin="2,5,5,2"
HorizontalAlignment="Left" >
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock MinWidth="80" HorizontalAlignment="Left" Grid.Column="0" Margin="5,2,2,2" Grid.Row ="0"
Text="{Binding Path=ColName}"/>
<ComboBox Name="cboColType" Grid.Column="1"
HorizontalAlignment="Right"
ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
SelectedItem="{Binding Path=ColumnType}"
Margin="2,2,2,2"
local:ItemSelectedBehavior.ItemSelected="{Binding Path=LoadConfigCommand}"
/>
</Grid>
</Border>
</HierarchicalDataTemplate>
I also tried creating a style
<Style x:Key="childItemStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="local:ItemSelectedBehavior.ItemSelected" Value="{Binding Path=LoadConfigCommand}" />
</Style>
and setting the itemcontainerstyle to the style in the hierarchical datatemplate..still no luck ..
<HierarchicalDataTemplate>
...
<ComboBox Name="cboColType" Grid.Column="1"
HorizontalAlignment="Right"
ItemsSource="{Binding Source={StaticResource dataFromEnum}}"
SelectedItem="{Binding Path=ColumnType}"
Margin="2,2,2,2"
ItemContainerStyle={StaticeResource childItemStyle}"
/>
...
</HierarchicalDataTemplate>
I'm still learning a lot about WPF so I'm assuming there is something particular about the hierchical datatemplate that is not allowing the attache dproperty to be set..I have found similar posts in the forums and tried to implement their solutions as above, but after a day of searching and experimenting wiht no luck I'm hoping some one has an idea about this...
© Stack Overflow or respective owner