绑定 WPF/XAML 用户控件的依赖属性

15

我的应用程序长这样:


SectionHeader

SectionHeader

Content

SectionHeader

Content


SectionHeader 是一个用户控件,具有两个依赖属性:Title 和 Apps。

Title 不会改变,但是 Apps 需要绑定到主窗口视图模型的 Apps 属性。Apps 属性仅适用于三个部分标题中的两个。

<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
   Apps="{Binding Path=Apps}" />

目前状况如下。问题在于应用程序没有显示出来。

SectionHeader 中,DataContext 被设置为自身,以便使标题显示出来。

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

Apps是UserControl中ItemsControl的ItemsSource:

<ItemsControl
      ItemsSource="{Binding Apps}">

所以我的问题是:

  • 如何将数据绑定到用户控件 DP?
  • 是否有不使用用户控件的更简单的方法来完成此布局?

编辑:

忘记提到 Apps 是 AppsItems 的 ObservableCollection。

这是我的 DP 内容:

public static readonly DependencyProperty AppsProperty = DependencyProperty.Register("Apps",
  typeof (ObservableCollection<AppsItem>), typeof (SectionHeader),
  new PropertyMetadata(null, OnAppsPropertyChanged));

private static void OnAppsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  Console.WriteLine("Hello!!!");
  var sectionHeader = d as SectionHeader;
  var value = e.NewValue as ObservableCollection<AppsItem>;
  sectionHeader.Apps = value;
}
4个回答

23

给你的用户控件起一个名字,然后尝试像这样进行绑定

ItemsSource="{Binding Apps,ElementName=root}"

而 root 是给你的 UserControl 命名的名称。

  <UserControl x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Height="350" Width="525"
     x:Name="root">

它是绑定到具有"Apps"属性的用户控件的元素。


Title 属性绑定得很好 - Apps 属性是问题所在。请参见问题编辑。 - vaughan
1
你确定你绑定在 "Apps="{Binding Path=Apps}" 的 Apps 属性与你的依赖属性 ObservableCollection<AppsItem> 是相同类型的吗?请确保你没有绑定类似 List<AppsItems> 或 IEnumerable<AppsItem> 这样的类型。它应该是类型为 ObservableCollection<AppsItem> 的。 - Bathineni
它现在可以工作了,但我不知道之前为什么不能工作 :/ 我的用户控件中有4个属性,但其中两个是有效的。加上这个后,它开始工作了...但我很好奇为什么之前不能工作 :/ - Khurram

11

在尝试从您的描述中重新创建此问题并遇到类似问题后,我发现我唯一能让它正常工作的方法是不设置UserControl的DataContext,而是使用ElementBinding:

<UserControl x:Class="WpfApplication2.UserControl1"
             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" 
             x:Name="thisUC"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Border Background="Red" Padding="10">
        <ListBox x:Name="ucList" ItemsSource="{Binding ElementName=thisUC, Path=UCApps}"/>
        </Border>

    </Grid>
</UserControl>

这个 Use Case 的实现非常简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public IEnumerable UCApps
        {
            get { return (IEnumerable)GetValue(UCAppsProperty); }
            set { SetValue(UCAppsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Apps.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty UCAppsProperty =
            DependencyProperty.Register("UCApps", typeof(IEnumerable), typeof(UserControl1), new UIPropertyMetadata(null));

        public UserControl1()
        {
            InitializeComponent();            
        }
    }
}

就是调用它的 XAML:

<Grid >
    <my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>

我将您的Usercontrol中Apps的名称更改为UCApps,这样我就可以看到发生了什么-太多具有相同名称的属性会让人感到困惑!


嘿,马克,感谢您的帮助。这个建议与上面的Bathineni类似,使用名为root的用户控件。最终这就是我需要做的 - 但我尝试绑定的集合也存在问题。 - vaughan

3
Apps="{Binding Path=Apps}"

绑定应用程序到自身。
请尝试以下操作:
1)您的ViewModel应该是控件的DataContext,然后您可以使用以下内容。
Apps="{Binding DataContext.Apps}"

您可以使用Snoop工具实时查找绑定问题。

似乎没有修复。用户控件中的应用程序 DP Setter 没有被触发。 - vaughan
@reckoner:Setter不会被触发,相反的,GetSet方法会被框架调用。如果您需要对依赖属性的更改做出反应,则需要添加回调函数。 - Phil Gan
@reckoner:这就是为什么建议不要在依赖属性的getter和setter中放置任何逻辑,因为WPF以其他方式调用依赖属性。 - sll
谢谢sll,我认为这更多与MainWindow和UserControl之间的交互有关。请参见问题编辑,因为我忘记了Apps是一个ObservableCollection。 - vaughan

1

你试过了吗?

Apps="{Binding Apps}" 

而不是

Apps="{Binding Path=Apps}"

在你的顶层控件中?

此外,当您运行应用程序时,是否在VS输出窗口中收到任何绑定错误?


没有绑定错误。应用程序与路径之间没有区别。奇怪的是,当我将Console.WriteLine(..)放置在Title getter和setter中时,它不会触发。 - vaughan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接