views:

225

answers:

1

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...

+1  A: 

found the answer to my own question in this post

and changed the code in the hierarchical datatemplate to search up the tree and find the custom attached property in the window datacontext. I was telling it to set the attached property on the local datacontext and the property only exists in the window datacontext.

changed my code to

<HierarchicalDataTemplate>
...
    <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 RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.LoadConfigCommand}"  
                                           />
...
</HierarchicalDataTemplate>

and it works. yay!

jesse_t_r