Horizo​​ntal和VerticalContentAlig

Horizo​​ntal和VerticalContentAlig

本文介绍了TreeViewItem中的绑定错误(Horizo​​ntal和VerticalContentAlignment)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这些错误.我已经阅读了几篇文章,并且TreeViewItem确实定义了Horizo​​ntal和VerticalContentAlignment.有趣的是,在.NET 4.0+中进行编译可以使错误消失(我使用3.5-否则我将遭受此 https://connect.microsoft.com/VisualStudio/feedback/details/588343/将更改后的行为从网络3-5-to-net-4-0的wpf文本框格式设置为适当的时间)

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错误:4:找不到与之绑定的源参考'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.ItemsControl',AncestorLevel ='1''. BindingExpression:Path = VerticalContentAlignment;DataItem = null;目标元素是'TreeViewItem'(Name ='');目标属性为"VerticalContentAlignment"(类型为"VerticalAlignment")

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

我想念什么?

谢谢安迪

推荐答案

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

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

这篇关于TreeViewItem中的绑定错误(Horizo​​ntal和VerticalContentAlignment)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:04