绑定到用户控件WPF / XAML的依赖属性 [英] Binding to a dependency property of a user control WPF/XAML

查看:534
本文介绍了绑定到用户控件WPF / XAML的依赖属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是这样的:






SectionHeader



SectionHeader



内容



SectionHeader



内容






SectionHeader 是两个依赖用户控件性能=标题和应用程序。



标题不会改变,但应用程序需要绑定到主窗口视图模型Apps的财产。该应用程序属性只需要两三个节头的



  c为C:SectionHeader DockPanel.Dock =顶部 X:NAME =SectionResources标题=资源
应用={绑定路径=应用}/>

这是怎么回事的时刻。问题是,应用程序没有显示出来。



SectionHeader 在DataContext如下设置本身这使得标题出现。

 的DataContext ={绑定的RelativeSource = {的RelativeSource自}}

应用程序是的ItemsSource为的ItemsControl 在该用户:

 < ItemsControl的
的ItemsSource ={结合应用}>






所以我的问题是:




  • 我的数据如何绑定到用户控件DP?

  • 是他们做此布局没有用户控件的一个更简单的方法?



编辑:



忘了提,Apps是AppsItems的一个ObservableCollection



这是我的DP是这样的:

 公共静态只读的DependencyProperty AppsProperty = DependencyProperty.Register(应用,
typeof运算(的ObservableCollection< AppsItem>)的typeof(SectionHeader),
新PropertyMetadata(NULL,OnAppsPropertyChanged));

私有静态无效OnAppsPropertyChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
{
Console.WriteLine(你好!);
VAR sectionHeader = D为SectionHeader;
VAR值= e.NewValue作为的ObservableCollection< AppsItem取代;
sectionHeader.Apps =价值;
}


解决方案

提供一个名称的usecontrol并尝试这样的结合

 的ItemsSource ={结合应用,的ElementName =根}

和根的名称是给你的用户控件

 <用户控件X:类=WpfApplication1.MainWindow
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =HTTP ://schemas.microsoft.com/winfx/2006/xaml
HEIGHT =350WIDTH =525
X:NAME =根>



这是元素结合到具有应用性质


的用户控件

My app looks like this:


SectionHeader

SectionHeader

Content

SectionHeader

Content


SectionHeader is a user control with two dependency properties = Title and Apps.

Title does not change but Apps needs to be bound to the main window view model Apps property. The Apps property is only required for two of the three section headers.

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

This is how it is at the moment. The problem is that the Apps don't show up.

In SectionHeader the DataContext is set to itself as follows which allows the Title to show up.

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

Apps is the ItemsSource for a ItemsControl in the UserControl:

<ItemsControl
      ItemsSource="{Binding Apps}">


So my questions are:

  • How can I data bind to a UserControl DP?
  • Is their an easier way of doing this layout without UserControls?

EDIT:

Forgot to mention that Apps is an ObservableCollection of AppsItems.

This is what my DP looks like:

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

解决方案

Give a name to your usecontrol and try binding like this

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

and root is the name give to your 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">

it is element binding to user control which has Apps property

这篇关于绑定到用户控件WPF / XAML的依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆