无法找到原因。代码:
<ComboBox OverridesDefaultStyle="True" MinWidth="70" MinHeight="20"
IsSynchronizedWithCurrentItem="True" ScrollViewer.CanContentScroll="True"
ToolTip="{Binding Text, Mode=TwoWay, ElementName=UserControl}"
IsEditable="{Binding IsEditable, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:SmartMultiTypeListView}}"
ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"
DataContext="{Binding ElementName=UserControl, Path=DataContext}"
IsEnabledChanged="OnIsEnabledChanged">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
的IsEnabled:
/// <summary>
/// Used to handle when the combobox is enabled/disabled
/// </summary>
private void OnIsEnabledChanged (object Sender, DependencyPropertyChangedEventArgs E)
{
if ((bool)E.NewValue == true)
{
ComboBox CBox = Sender as ComboBox;
if (CBox != null)
{
IEnumerable LastBinding = CBox.ItemsSource; // Store old binding
CBox.ItemsSource = null; // Forces refresh/rebinding
CBox.ItemsSource = LastBinding; // Reset binding by rebinding the old value
}
}
}
似乎相当无害,但我在施工期间收到错误号码4,HorizontalContentAlignment
收到错误,VerticalContentAlignment
收错:
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 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
无法在SO上找到任何有效的解决方案。我曾尝试覆盖ListViewItem
,ComboBoxItem
和ListBoxItem
两种风格,但它并不起作用。错误本身只发生20%的时间,这使得跟踪更加困难。有时,很少,错误似乎发生在程序的最后(很多次,20次以上)。以上是我可以从UserControl
中提取的MVCE。
造成这种情况的原因是什么?
修改
完整的XAML:
<ComboBox OverridesDefaultStyle="True" MinWidth="70" MinHeight="20"
IsSynchronizedWithCurrentItem="True" ScrollViewer.CanContentScroll="True"
ToolTip="{Binding Text, Mode=TwoWay, ElementName=UserControl}"
IsEditable="{Binding IsEditable, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:SmartMultiTypeListView}}"
ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"
DataContext="{Binding ElementName=UserControl, Path=DataContext}"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Center"
IsEnabledChanged="OnIsEnabledChanged">
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemTemplate>
<HierarchicalDataTemplate>
<CheckBox Content="{Binding Name}" Margin="2" IsChecked="{Binding IsChecked, Mode=TwoWay}"
IsEnabled="{Binding IsEnabled}" Tag="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}" />
</HierarchicalDataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.Template>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Grid.Column="2" Focusable="False"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"/>
<ContentPresenter x:Name="Presenter" IsHitTestVisible="False" Margin="3, 3, 23, 3" VerticalAlignment="Center"
HorizontalAlignment="Left" >
<ContentPresenter.Content>
<TextBlock TextTrimming="CharacterEllipsis" Foreground="Black"
Text="{Binding Path=Text, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
</ContentPresenter.Content>
</ContentPresenter>
<TextBox x:Name="EditableTextBox" Style="{x:Null}" HorizontalAlignment="Left"
VerticalAlignment="Center" Margin="3, 3, 23, 3" Focusable="True" Background="Transparent" Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}" />
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"
Focusable="False" PopupAnimation="None">
<Grid SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" Background="{StaticResource WindowBackgroundBrush}" BorderThickness="1"
BorderBrush="#444" />
<ScrollViewer Margin="4, 6, 4, 6" SnapsToDevicePixels="True" DataContext="{Binding}">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}" />
</Trigger>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false" />
</Trigger>
<Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
<Setter TargetName="DropDownBorder" Property="Margin" Value="0, 2, 0, 0" />
</Trigger>
<Trigger Property="IsEditable" Value="False">
<Setter Property="IsEnabled" Value="False" />
<Setter Property="IsTabStop" Value="False" />
<Setter TargetName="EditableTextBox" Property="Visibility" Value="Visible" />
<Setter TargetName="Presenter" Property="Visibility" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ComboBox.Template>
</ComboBox>
修改
最新错误:
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 'ListViewItem' (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 'ListViewItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
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 'ListBoxItem' (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 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
我省略了重复的错误(其中我得到了~20,所有类似于后者2)。
答案 0 :(得分:1)
HorizontalContentAlignment
或VerticalContentAlignment
上不存在ComboBoxItem
和ListViewItem
属性。您有两个选择:
ComboBox
尝试删除这些:
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
并将以下内容放在ComboBox元素中(例如):
<ComboBox OverridesDefaultStyle="True" MinWidth="70" MinHeight="20"
IsSynchronizedWithCurrentItem="True" ScrollViewer.CanContentScroll="True"
ToolTip="{Binding Text, Mode=TwoWay, ElementName=UserControl}"
IsEditable="{Binding IsEditable, Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:SmartMultiTypeListView}}"
ItemsSource="{Binding ElementName=UserControl, Path=ItemsSource}"
DataContext="{Binding ElementName=UserControl, Path=DataContext}"
IsEnabledChanged="OnIsEnabledChanged"
HorizontalContentAlignment="Left"
VerticalContentAlignment="Center">
改变这个:
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
要:
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />