开发者

Can't use attached property on combobox inside hierarchical datatemplate WPF

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


Found the answer to my own question in here:

Command Binding in hierarchical datatemplate

And changed the code in the HierarchicalDataTemplate 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"
              local:ItemSelectedBehavior.ItemSelected="{Binding 
              RelativeSource={RelativeSource FindAncestor, 
                              AncestorType={x:Type Window}}, Path=DataContext.LoadConfigCommand}" />

...

</HierarchicalDataTemplate>

And it works. Yay!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜