无法在分层数据模板 WPF 内的组合框上使用附加属性
我希望使用附加属性将命令分配给嵌入树视图内的组合框的选择更改事件。我试图在树的分层数据模板内设置附加属性,但该命令未设置,并且当组合框中的项目更改时不会触发。
我发现直接在数据模板外部的组合框中设置附加属性效果很好;
以下是我尝试在模板中设置属性的方法:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里找到我自己的问题的答案:
分层数据模板中的命令绑定
并更改了
HierarchicalDataTemplate
中的代码来搜索树并在 WindowDataContext
中查找自定义附加属性。我告诉它在本地DataContext
上设置附加属性,并且该属性仅存在于 WindowDataContext
中。将我的代码更改为:
并且它有效。耶!
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 WindowDataContext
. I was telling it to set the attached property on the localDataContext
and the property only exists in the WindowDataContext
.Changed my code to:
And it works. Yay!