我在一个VS 2012解决方案中有3个项目。下面是抛出此错误的XAML文件在项目A中。 BmpImage.cs
文件存在于同一解决方案中的不同项目B中,该解决方案包含所有扩展和帮助文件,并包含所需的所有方法和代码。项目A参考项目B.
任何帮助,请将我的工作放在这里。我被卡住了。我已经阅读了所有其他类似的问题,但没有一个解决这个问题
我收到此错误:
System.Windows.Data错误:4:找不到绑定源 参考'RelativeSource FindAncestor, AncestorType ='System.Windows.Controls.Button',AncestorLevel ='1''。 BindingExpression:路径=的IsEnabled;的DataItem = NULL;目标元素是 'BmpImage'(Name =''); target属性是'NoTarget'(类型'Object')
<UserControl x:Class="MyGrid.MyPanel"
Name="_ctrl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ex="clr-namespace:Helper.Xaml;assembly=Helper.Xaml"
xmlns:sx="clr-namespace:MyPanel.Xaml;assembly=MyPanel.Xaml">
<UserControl.Resources>
**<Style x:Key="BmpStyle" TargetType="{x:Type ex:BmpImage}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
<Setter Property="UIElement.Opacity" Value="0.3" />
</DataTrigger>
</Style.Triggers>
</Style>**
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ToolBar ToolBarTray.IsLocked="True">
<Button ToolTip="Filter..." ToolTipService.ShowOnDisabled="True" Click="Filter_OnClick">
<ex:BmpImage Source="Images/filter.png" Style="{StaticResource BmpStyle}" />
</Button>
<ToggleButton ToolTip="AutoScroll" ToolTipService.ShowOnDisabled="True" IsChecked="{Binding ElementName=Trades, Path=AutoScroll}">
<ex:BmpImage Source="Images/Autoscroll.png" Style="{StaticResource BmpStyle}" />
</ToggleButton>
</ToolBar>
<sx:PanelGrid Name="PanelGrid" Grid.Row="1" />
</Grid>
</UserControl>
答案 0 :(得分:0)
您的代码看起来是正确的...您是否尝试过Binding
表达而没有可选的AncestorLevel=1
?我通常使用这种Binding
,但我从不打扰AncestorLevel
属性,因为如果设置错误,或者如果XAML被更改,可能会导致错误
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type
Button}}, Path=IsEnabled}" Value="False">
<Setter Property="UIElement.Opacity" Value="0.3" />
</DataTrigger>
答案 1 :(得分:0)
使用以下代码:
using DevExpress.Xpf.Core.Native;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace BindingErrorHelper
{
public class IsTypeFoundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
FrameworkElement element = value as FrameworkElement;
Type type = parameter as Type;
if (element != null && type != null)
{
element = LayoutHelper.FindElement(element, p => p.GetType() == type);
if (element != null)
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
public class LayoutHelper
{
public static FrameworkElement FindElement(FrameworkElement treeRoot, System.Predicate<FrameworkElement> predicate)
{
VisualTreeEnumerator visualTreeEnumerator = new VisualTreeEnumerator(treeRoot);
while (visualTreeEnumerator.MoveNext())
{
FrameworkElement frameworkElement = visualTreeEnumerator.Current as FrameworkElement;
if (frameworkElement != null && predicate(frameworkElement))
{
return frameworkElement;
}
}
return null;
}
}
}
将XAML代码写为:
<tt:IsTypeFoundConverter x:Key="isTypeFoundConverter"/>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource isTypeFoundConverter}, ConverterParameter={x:Type Button}}" Value="true"/>
<Condition Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=IsEnabled}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="UIElement.Opacity" Value="0.3" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>