Xaml 产生路径错误,但有些仍然有效

Posted

技术标签:

【中文标题】Xaml 产生路径错误,但有些仍然有效【英文标题】:Xaml producing path errors but some still work 【发布时间】:2015-01-14 15:02:21 【问题描述】:

我遇到了绑定路径错误的情况。但是,一些绑定仍然有效,而另一些则无效。我有以下课程(仅复制/粘贴相关位):

后面的代码:

class RSSManager : BaseViewModel

    #region Resources
    private Platforms _platformsList;

    public Platforms PlatformsList
    
        get  return _platformsList; 
        set
        
            _platformsList = value;
            NotifyPropertyChanged("PlatformsList");
        
    

    private List<IActivity> _mainActivitiesList = new List<IActivity>();

    public List<IActivity> MainActivitiesList
    
        get  return _mainActivitiesList; 
        set
        
            _mainActivitiesList = value;
            NotifyPropertyChanged("MainActivitiesList");
        
    

    private ObservableCollection<IActivity> _activitiesList = new ObservableCollection<IActivity>();

    public ObservableCollection<IActivity> ActivitiesList
    
        get  return _activitiesList; 
        set
        
            _activitiesList = value;
            NotifyPropertyChanged("ActivitiesList");
        
    

    private ObservableCollection<IActivity> _alertsList = new ObservableCollection<IActivity>();

    public ObservableCollection<IActivity> AlertsList
    
        get  return _alertsList; 
        set
        
            _alertsList = value;
            NotifyPropertyChanged("AlertsList");
        
    

    private ObservableCollection<IActivity> _invasionsList = new ObservableCollection<IActivity>();

    public ObservableCollection<IActivity> InvasionsList
    
        get  return _invasionsList; 
        set
        
            _invasionsList = value;
            NotifyPropertyChanged("InvasionsList");
        
    

    private ObservableCollection<IActivity> _outbreaksList = new ObservableCollection<IActivity>();

    public ObservableCollection<IActivity> OutbreaksList
    
        get  return _outbreaksList; 
        set
        
            _outbreaksList = value;
            NotifyPropertyChanged("OutbreaksList");
        
    

    private ObservableCollection<IActivity> _doneList = new ObservableCollection<IActivity>();

    public ObservableCollection<IActivity> DoneList
    
        get  return _doneList; 
        set
        
            _doneList = value;
            NotifyPropertyChanged("DoneList");
        
    

    #endregion

    public partial class Activities : Page, IOptions

    Options.OptionsActivities _options = new Options.OptionsActivities();

    RSSManager _manager = new RSSManager();

    public Activities()
    
        InitVars();
        InitializeComponent();
        DataContext = _manager;
        UpdateCheckBoxes();
        UpdateTabsHeaders();
    

Activity.xaml:

<Page x:Class="Warframe_Activity_Manager.Views.Activities"
  DataContext="Binding Source=RelativeSource Self"
  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" 
  mc:Ignorable="d" 
  d:DesignHeight="718" d:DesignWidth="1024"
  GotFocus="Page_GotFocus"
Title="Activities">

<Grid Name="MainGrid" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="30" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <StackPanel Name="PlatformChoices" Orientation="Horizontal" Background="Transparent">
        <Label Name="ChoicePCLabel" Content="Show PC Activities" Foreground="Aqua" VerticalAlignment="Center"></Label>
        <CheckBox Name="ChoicePC" VerticalAlignment="Center" Click="Platform_Click"/>
        <Label Name="ChoicePS4Label" Content="Show PlayStation 4 Activities" Foreground="Aqua" VerticalAlignment="Center"></Label>
        <CheckBox Name="ChoicePS4" VerticalAlignment="Center" Click="Platform_Click"/>
        <Label Name="ChoiceXB1Label" Content="Show XBox One Activities" Foreground="Aqua" VerticalAlignment="Center"></Label>
        <CheckBox Name="ChoiceXB1" VerticalAlignment="Center" Click="Platform_Click"/>
    </StackPanel>
    <Button Name="Refresh" HorizontalAlignment="Right" Content="Refresh" Click="Refresh_Click"/>
    <TabControl Name="Tabs" Grid.Row="1" Background="Transparent">
        <TabItem Name="TabAll" Header="All Activities" BorderThickness="0">
            <ListView Name="ListAll" Background="Transparent" ItemsSource="Binding ActivitiesList" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="1*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="1*"/>
                                <RowDefinition Height="1*"/>
                            </Grid.RowDefinitions>
                            <StackPanel Name="ActivityTypeContainer" Orientation="Horizontal" HorizontalAlignment="Left">
                                <Label Name="ActivityTypeLabel" VerticalAlignment="Center" Foreground="Red" Content="Activity Type :"/>
                                <Label Name="ActivityType" VerticalAlignment="Center" Foreground="Red" Content="Binding Path=Type"/>
                            </StackPanel>
                            <StackPanel Name="ActivityPlatformContainer" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0, 0, 10, 0">
                                <Label Name="ActivityPlatformLabel"  VerticalAlignment="Center" Foreground="Blue" Content="Platform :"/>
                                <Label  Name="ActivityPlatform" VerticalAlignment="Center" Foreground="Blue" Content="Binding Path=Platform"/>
                            </StackPanel>
                            <StackPanel Name="ActivityInfoContainer" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Left">
                                <Label Name="ActivityInfoLabel" VerticalAlignment="Center" Foreground="Orange" Content="Activity Information : "/>
                                <Label Name="ActivityInfo" VerticalAlignment="Center" Foreground="Orange" Content="Binding Path=Info"/>
                            </StackPanel>
                            <StackPanel Name="ActivityDoneContainer" Grid.Column="1" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"  Margin="0, 0, 10, 0">
                                <Label Name="ActivityDoneLabel" VerticalAlignment="Center" Foreground="Green" Content="Finished : "/>
                                <CheckBox Name="ActivityDone" VerticalAlignment="Center" IsChecked="Binding Path=Done, Mode=TwoWay" Click="ActvityDone_Click"/>
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </TabItem>
    </TabControl>
</Grid>

错误:

System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RelativeSource”(HashCode=16143157)上找不到“ActivitiesList”属性。绑定表达式:路径=活动列表; DataItem='RelativeSource' (HashCode=16143157);目标元素是“ListView”(名称=“ListAll”);目标属性是“ItemsSource”(类型“IEnumerable”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RelativeSource”(HashCode=16143157)上找不到“AlertsList”属性。绑定表达式:路径=警报列表; DataItem='RelativeSource' (HashCode=16143157);目标元素是“ListView”(名称=“ListAlerts”);目标属性是“ItemsSource”(类型“IEnumerable”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RelativeSource”(HashCode=16143157)上找不到“InvasionsList”属性。绑定表达式:路径=入侵列表; DataItem='RelativeSource' (HashCode=16143157);目标元素是'ListView'(名称='ListInvasions');目标属性是“ItemsSource”(类型“IEnumerable”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RelativeSource”(HashCode=16143157)上找不到“OutbreaksList”属性。绑定表达式:路径=爆发列表; DataItem='RelativeSource' (HashCode=16143157);目标元素是“ListView”(名称=“ListOutbreaks”);目标属性是“ItemsSource”(类型“IEnumerable”) System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“RelativeSource”(HashCode=16143157)上找不到“DoneList”属性。绑定表达式:路径=完成列表; DataItem='RelativeSource' (HashCode=16143157);目标元素是“ListView”(名称=“ListDone”);目标属性是“ItemsSource”(类型“IEnumerable”)

我还没有进入分解模板的部分,但我认为这不是这里的问题。

如前所述,即使我收到错误,程序也会显示我想要的内容。但是,我接下来添加的绑定根本无法使用给出相同错误类型(错误路径)的输出。我在这里做错了什么?

【问题讨论】:

您在 XAML 中设置 DataContext,也在后端代码的构造函数中设置。我认为这就是你的错误所在。删除 XAML 中的 DataContext 行,这应该会为您完成。 这就像一个魅力(虽然我不知道为什么它仍然有效)。我想验证你的答案,你能把它作为答案而不是评论吗? 没问题。绑定有时可能很难调试。 :-) 【参考方案1】:

您正在 XAML 和构造函数中设置 DataContext。

删除 XAML 中的 DataContext 行,因为这会导致输出错误:

DataContext="Binding Source=RelativeSource Self"

在绑定失败的情况下,应用程序不会引发任何错误并停止应用程序,但它们会在输出中显示它们。

我发现在运行应用程序之前查看任何绑定错误的好方法是检查智能感知是否正常工作。如果是,那么你应该很高兴。

【讨论】:

顺便问一下,我如何检查智能感知是否工作? 在 XAML 中键入绑定时,对象中的属性应该在智能感知列表中。 嗯,这很奇怪,因为即使现在我也无法在列表中找到我的对象的属性。除非您在 WPF 对象中谈论“绑定”、“路径”、“模式”和所有其他内容。

以上是关于Xaml 产生路径错误,但有些仍然有效的主要内容,如果未能解决你的问题,请参考以下文章

如何在 XAML 中为 WinRT 中的贝塞尔路径设置动画

Spring Boot with docker 无法找到请求目标错误的有效认证路径

如果 gem 源指定为 git,则 ActiveMerchant ActionView 帮助程序初始化崩溃,但如果它是默认路径或本地路径则有效

android:无法找到请求目标的有效认证路径

WPF - 路径/几何帮助 - 奇怪的形状

无法弄清楚为什么我的部分复制命令有效,有些则不然