TreeViewItem中的绑定错误(Horizo​​ntal和VerticalContentAlignment)

时间:2013-12-27 00:01:44

标签: .net wpf binding treeviewitem

我收到了这些错误。我已阅读了几篇帖子,我的TreeViewItem确实定义了Horizo​​ntal和VerticalContentAlignment。有趣的是,在.NET 4.0+中进行编译会使错误消失(我使用3.5 - 否则我会受此影响https://connect.microsoft.com/VisualStudio/feedback/details/588343/changed-behaviour-from-net-3-5-to-net-4-0-of-wpf-textbox-formatting-when-proper

  

System.Windows.Data错误:4:找不到绑定源   参考'RelativeSource FindAncestor,   AncestorType = 'System.Windows.Controls.ItemsControl',   AncestorLevel = '1'”。 BindingExpression:路径= Horizo​​ntalContentAlignment;   的DataItem = NULL; target元素是'TreeViewItem'(Name ='');目标   property是'Horizo​​ntalContentAlignment'(类型'Horizo​​ntalAlignment')

     

System.Windows.Data错误:4:找不到绑定源   参考'RelativeSource FindAncestor,   AncestorType = 'System.Windows.Controls.ItemsControl',   AncestorLevel = '1'”。 BindingExpression:路径= VerticalContentAlignment;   的DataItem = NULL; target元素是'TreeViewItem'(Name ='');目标   property是'VerticalContentAlignment'(类型'VerticalAlignment')

<!-- TreeViewItem templates (full length parent items) -->
        <Style x:Key="TreeViewItemStyleBase" TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="True"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="19" Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="5"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <ToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ExpandCollapseToggleStyle}"/>
                            <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="1" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                                <ContentPresenter x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.Row="1"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="False">
                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="False">
                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource CustomLightHighlight}"/>
                                <Setter Property="Foreground" Value="{StaticResource CustomWhite}"/>
                            </Trigger>                            
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="{StaticResource CustomGray}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>            
        </Style>

    <!-- Data templates-->
        <HierarchicalDataTemplate x:Key="RealTemplate" DataType="{x:Type l:Account}" ItemsSource="{Binding Path=children}">
            <DockPanel LastChildFill="True">
                <TextBlock x:Name="AccountTitle" Text="{Binding Path=title}" Foreground="White" DockPanel.Dock="Left"/>
                <TextBox x:Name="EditAccountTitle" Text="{Binding Path=title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource RoundedTextBox}" FontWeight="Bold" LostFocus="tvLostFocus" PreviewKeyDown="tvKeyDown" LostKeyboardFocus="tvLostFocus" Visibility="Collapsed" DockPanel.Dock="Left" l:FocusExtension.IsFocused="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}}" CaretIndex="{x:Static sys:Int32.MaxValue}"/>
                <TextBlock Text="{Binding Path=amount}" Foreground="{Binding Converter={StaticResource GetColourConverterAmount}}" DockPanel.Dock="Right" TextAlignment="Right"/>                
            </DockPanel>
            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=isEditable}" Value="True">
                    <Setter TargetName="AccountTitle" Property="Visibility" Value="Collapsed"/>
                    <Setter TargetName="EditAccountTitle" Property="Visibility" Value="Visible"/>                                        
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=isEditable}" Value="False">
                    <Setter TargetName="AccountTitle" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="EditAccountTitle" Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource TreeViewTopItemConverter}}" Value="False">
                    <DataTrigger.Setters>
                        <Setter Property="ContextMenu" Value="{StaticResource RealAccountMenu}"/>
                    </DataTrigger.Setters>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource TreeViewTopItemConverter}}" Value="True">
                    <DataTrigger.Setters>
                        <Setter Property="ContextMenu" Value="{StaticResource CategoryMenu}"/>
                    </DataTrigger.Setters>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>

<!-- TreeViewItem styles - these styles defined here as reference code -->        
        <Style x:Key="TreeViewItemStyleReal" BasedOn="{StaticResource TreeViewItemStyleBase}" TargetType="{x:Type TreeViewItem}">
            <Setter Property="HeaderTemplate" Value="{StaticResource RealTemplate}"/>
            <Setter Property="IsExpanded" Value="{Binding isNodeExpanded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <Setter Property="IsSelected" Value="{Binding isNodeSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>

            <!-- Events -->
            <EventSetter Event="TreeViewItem.MouseDoubleClick" Handler="tvShowItems"/>
            <EventSetter Event="TreeViewItem.PreviewMouseLeftButtonDown" Handler="tvMouseDown"/>
            <EventSetter Event="TreeViewItem.PreviewMouseRightButtonUp" Handler="tvMouseRightDown"/>
            <EventSetter Event="TreeViewItem.PreviewMouseMove" Handler="tvMouseMove"/>
            <EventSetter Event="TreeViewItem.Drop" Handler="tvDrop"/>                        
            <EventSetter Event="TreeViewItem.Expanded" Handler="tvExpanded"/>
            <EventSetter Event="TreeViewItem.Collapsed" Handler="tvCollapsed"/>
        </Style>

我错过了什么?

由于 安迪

1 个答案:

答案 0 :(得分:3)

找出答案。我没有在集合中正确实现INotifyPropertyChanged,因此使用CollectionViewSource刷新刷新(导致重新绘制TreevView),我认为必须打破绑定。由于正确实施了通知问题已解决