无法在分层数据模板 WPF 内的组合框上使用附加属性

发布于 2024-09-03 04:33:24 字数 2848 浏览 5 评论 0原文

我希望使用附加属性将命令分配给嵌入树视图内的组合框的选择更改事件。我试图在树的分层数据模板内设置附加属性,但该命令未设置,并且当组合框中的项目更改时不会触发。

我发现直接在数据模板外部的组合框中设置附加属性效果很好;

以下是我尝试在模板中设置属性的方法:

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

我还尝试创建样式

<Style x:Key="childItemStyle" TargetType="{x:Type FrameworkElement}">
        <Setter Property="local:ItemSelectedBehavior.ItemSelected" Value="{Binding Path=LoadConfigCommand}" />

    </Style>

并将 itemcontainerstyle 设置为分层数据模板中的样式..仍然没有运气..

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

我仍在学习很多有关 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2024-09-10 04:33:24

在这里找到我自己的问题的答案:

分层数据模板中的命令绑定

并更改了HierarchicalDataTemplate 中的代码来搜索树并在 Window DataContext 中查找自定义附加属性。我告诉它在本地 DataContext 上设置附加属性,并且该属性仅存在于 Window DataContext 中。

将我的代码更改为:

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

并且它有效。耶!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文