user2712741
user2712741

Reputation:

System.Windows.Data Error: 4 : Cannot find source for binding with reference

I have 3 Projects in a single VS 2012 solution. Below is the XAML file that is throwing this error is in Project A. The BmpImage.cs file exists in different project B in the same solution which contains all the extensions and helper files and contains all methods and code that is required. Project A references Project B.

Anyone could help, please? My job is on the line here but I am stuck. I have read all other similar questions but none does address this issue.

I am getting this error :

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

Upvotes: 4

Views: 12830

Answers (2)

Amol Bavannavar
Amol Bavannavar

Reputation: 2062

Use following Code :

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;
        }
    }
}

Write the XAML Code as :

<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> 

Upvotes: 1

Sheridan
Sheridan

Reputation: 69959

Your code looks about right... have you tried the Binding expression without the optional AncestorLevel=1? I generally use this kind of Binding, but I never bother with the AncestorLevel property because it could cause an error if it is set wrong, or if the XAML was ever changed.

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type 
    Button}}, Path=IsEnabled}" Value="False">
    <Setter Property="UIElement.Opacity" Value="0.3" />
</DataTrigger>

Upvotes: 1

Related Questions