Binding Errors in TreeViewItem (Horizontal and VerticalContentAlignment)

吃可爱长大的小学妹 提交于 2020-06-28 06:53:09

问题


I am getting these errors. I have read several posts and my TreeViewItem does have Horizontal and VerticalContentAlignment defined. Interestingly compiling in .NET 4.0+ makes the error go away (I use 3.5 - else I suffer from this 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 Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'TreeViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'TreeViewItem' (Name=''); target property is 'VerticalContentAlignment' (type '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>

What am I missing??

Thanks Andy


回答1:


Figured out the answer. I wasn't properly implementing INotifyPropertyChanged in the collection and as a result using CollectionViewSource Refresh to refresh (which resulted in re-draw of the TreevView) which I think must have broken the bindings. Since properly implementing the notification the problem has resolved



来源:https://stackoverflow.com/questions/20792897/binding-errors-in-treeviewitem-horizontal-and-verticalcontentalignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!