【问题标题】:BindingExpression path error: property not foundBindingExpression 路径错误:找不到属性
【发布时间】:2016-01-13 08:58:36
【问题描述】:

BusyIndi​​cator.xaml

<UserControl x:Class="Eigen.Modules.Modelling.Components.BusyIndicator"
             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:local="clr-namespace:Eigen.Modules.Modelling.Components"
             xmlns:telerikControls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
             xmlns:gif="http://wpfanimatedgif.codeplex.com"
             mc:Ignorable="d" 
             d:DesignHeight="150" d:DesignWidth="150"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <Grid Width="{Binding Width}" Height="{Binding Height}">
        <telerikControls:RadBusyIndicator IsIndeterminate="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=IsIndeterminate}">
            <Border BorderBrush="White" Background="#A9FFFFFF" BorderThickness="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Border Padding="5,0" Grid.Row="0">
                        <Image gif:ImageBehavior.AnimatedSource="/`enter code here`Resources/matriksenerji.gif" gif:ImageBehavior.AutoStart="True"/>
                    </Border>

                    <Border Padding="5" Grid.Row="1">
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=Label}" FontWeight="SemiBold" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center"/>
                    </Border>
                </Grid>
            </Border>
        </telerikControls:RadBusyIndicator>
    </Grid>
</UserControl>

BusyIndi​​cator.xaml.cs

public partial class BusyIndicator : UserControl
{
    public BusyIndicator()
    {
        InitializeComponent();
    }



    public string Label
    {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Label.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty LabelProperty =
        DependencyProperty.Register("Label", typeof(string), typeof(MatriksBusyIndicator), new PropertyMetadata(string.Empty));



    public bool IsIndeterminate
    {
        get { return (bool)GetValue(IsIndeterminateProperty); }
        set { SetValue(IsIndeterminateProperty, value); }
    }

    // Using a DependencyProperty as the backing store for IsProgress.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsIndeterminateProperty =
        DependencyProperty.Register("IsIndeterminate", typeof(bool), typeof(MatriksBusyIndicator), new PropertyMetadata(true));

}

在 Page.xaml 中使用

...

<!--BusyIndicator-->
<components:BusyIndicator IsIndeterminate="True" 
                          Visibility="{Binding IsProgress, Converter={StaticResource BoolToVisConverter}}" 
                          Label="{lex:Loc CalculatingProgress}"
                          Grid.Column="1" 
                          VerticalAlignment="Center" 
                          HorizontalAlignment="Center" 
                          Width="150" Height="150"/>

...

错误信息;

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“BusyIndi​​cator”上找不到“IsProgress”属性 (名称='')'。 BindingExpression:Path=DataContext.IsProgress; DataItem='BusyIndi​​cator'(名称='');目标元素是“BusyIndi​​cator” (名称='');目标属性是“可见性”(类型“可见性”)

ViewModel 类具有“IsProgress”属性,但无法绑定到用户控件。

【问题讨论】:

  • Page.xaml 的数据上下文是什么?
  • 一个视图模型类。 @daryal

标签: c# wpf xaml user-controls


【解决方案1】:

在 UserControl 定义中,您有以下行:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

这意味着该控件的 DataContext 是控件本身。所以 WPF 试图在这个控件中找到 IsProgress 属性。 据我了解,IsProgress 属性是在您的 Page 的 ViewModel 中定义的。

所以修复它的最佳选择就是删除上面的行。

另外要修复控件中的 Width 和 Height 绑定,请将 RelativeSource={RelativeSource AncestorType={x:Type UserControl}} 添加到它们。

【讨论】:

    【解决方案2】:

    你可以试一试:

    Visibility="{Binding IsProgress, , diag:PresentationTraceSources.TraceLevel=High, Converter={StaticResource BoolToVisConverter}}" 
    

    我建议你看看这里: debugging WPF binding problems

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      相关资源
      最近更新 更多