首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >绑定到用户控件WPF/XAML的依赖项属性

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

Stack Overflow用户
提问于 2011-07-22 08:08:31
回答 4查看 31.9K关注 0票数 15

我的应用程序看起来是这样的:

SectionHeader

SectionHeader

内容

SectionHeader

内容

SectionHeader是一个具有两个依赖项属性= Title和Apps的用户控件。

标题不会更改,但是应用程序需要绑定到主窗口视图模型Apps属性。仅对三个区段标题中的两个使用Apps属性。

代码语言:javascript
复制
<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
   Apps="{Binding Path=Apps}" />

目前的情况就是这样。问题是应用程序没有出现。

SectionHeader中,DataContext设置为自己,如下所示,允许显示标题。

代码语言:javascript
复制
DataContext="{Binding RelativeSource={RelativeSource Self}}"

应用程序是ItemsSource,用于UserControl中的ItemsControl

代码语言:javascript
复制
<ItemsControl
      ItemsSource="{Binding Apps}">

所以我的问题是:

  • 如何将数据绑定到UserControl DP?
  • 在没有UserControls的情况下,他们更容易完成这个布局吗?

编辑:

忘了提到Apps是ObservableCollection of AppsItems。

这就是我的DP的样子:

代码语言:javascript
复制
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;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-07-22 09:29:50

给您的usecontrol命名,并尝试像这样绑定

代码语言:javascript
复制
ItemsSource="{Binding Apps,ElementName=root}"

根目录是用户控件的名称。

代码语言:javascript
复制
  <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属性的用户控件的元素。

票数 23
EN

Stack Overflow用户

发布于 2011-07-22 14:16:29

在尝试从您的描述中复制这一点之后,遇到了类似的问题,我发现使其工作的唯一方法是不设置UserControl的UserControl,而是使用ElementBinding:

代码语言:javascript
复制
<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>

这个UC的实现非常简单:

代码语言:javascript
复制
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的XAML一样:

代码语言:javascript
复制
<Grid >
    <my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>

(我将用户控件中的Apps更改为UCApps,这样我就可以看到发生了什么--太多相同名称的属性使人感到困惑!)

票数 11
EN

Stack Overflow用户

发布于 2011-07-22 08:13:20

代码语言:javascript
复制
Apps="{Binding Path=Apps}"

将应用程序绑定到自己身上。

试着跟随

1)您的ViewModel应该是控制的DataContext,然后您可以使用以下方法

代码语言:javascript
复制
Apps="{Binding DataContext.Apps}"

此外,还可以使用斯诺普实用程序实时查找绑定问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6787219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档