C# 绑定到用户控件WPF/XAML的依赖项属性
我的应用程序如下所示:C# 绑定到用户控件WPF/XAML的依赖项属性,c#,wpf,xaml,C#,Wpf,Xaml,我的应用程序如下所示: SectionHeader SectionHeader 内容 SectionHeader 内容 SectionHeader是一个用户控件,具有两个依赖属性=Title和Apps 标题不会更改,但应用程序需要绑定到主窗口视图模型应用程序属性。Apps属性仅适用于三个节标题中的两个 <c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES" Apps="{B
SectionHeader
SectionHeader
内容
SectionHeader
内容
SectionHeader
是一个用户控件,具有两个依赖属性=Title和Apps
标题不会更改,但应用程序需要绑定到主窗口视图模型应用程序属性。Apps属性仅适用于三个节标题中的两个
<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
Apps="{Binding Path=Apps}" />
Apps是UserControl中ItemsControl
的项目资源:
<ItemsControl
ItemsSource="{Binding Apps}">
因此,我的问题是:
- 如何将数据绑定到UserControl 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;
}
public静态只读DependencyProperty AppsProperty=DependencyProperty.Register(“应用程序”),
typeof(ObservableCollection),typeof(SectionHeader),
新的PropertyMetadata(null,OnAppsPropertyChanged));
AppPropertyChanged上的私有静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
Console.WriteLine(“你好!!!”);
var sectionHeader=d作为sectionHeader;
var值=e.作为可观测集合的新值;
sectionHeader.Apps=值;
}
将应用程序绑定到自身
试着跟随
1) ViewModel应该是控件的DataContext,然后可以使用
Apps=“{Binding DataContext.Apps}”
您还可以使用实用程序实时查找绑定问题为usecontrol命名,然后尝试这样绑定
ItemsSource="{Binding Apps,ElementName=root}"
root是用户控件的名称
<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属性的用户控件的元素您尝试过吗
Apps="{Binding Apps}"
而不是
Apps="{Binding Path=Apps}"
在你最高层的控制之下
此外,在运行应用程序时,VS输出窗口中是否存在任何绑定错误 在尝试从您的描述中重新编写此内容并遇到类似问题后,我发现唯一可行的方法是不设置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>
此UC的实现非常简单:
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Collections.ObjectModel;
使用系统集合;
命名空间WpfApplication2
{
///
///UserControl1.xaml的交互逻辑
///
公共部分类UserControl1:UserControl
{
公共可数UCAPP
{
获取{return(IEnumerable)GetValue(UCAppsProperty);}
集合{SetValue(UCAppsProperty,value);}
}
//使用DependencyProperty作为应用程序的后台存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性UCAppsProperty=
DependencyProperty.Register(“UCApps”、typeof(IEnumerable)、typeof(UserControl1)、新UIPropertyMetadata(null));
公共用户控制1()
{
初始化组件();
}
}
}
正如调用它的XAML一样:
<Grid >
<my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>
(我将用户控件中的
Apps
的名称更改为UCApps
,这样我就可以看到发生了什么-太多同名的属性变得混乱!)似乎没有解决问题。UserControl中Apps DP的Setter没有被激发。@清算者:Setter不会被激发,而是框架调用get
和Set
方法。如果您需要对更改的依赖项属性做出反应,则需要添加回调。@计算者:这就是为什么建议不要在DPs的getter和setter中放置任何逻辑,因为WPF以其他方式调用DPs感谢sll,我认为这更多地与主窗口和UserControl之间的交互有关。查看问题编辑,因为我忽略了应用程序是一个可观察集合的事实。Title属性的绑定很好-应用程序属性就是问题所在。请参阅问题编辑。您确定应用程序属性“Apps=“{binding Path=Apps}”中绑定的内容吗“与依赖项属性(即ObservableCollection)的类型相同。确保您没有绑定列表或IEnumerable之类的东西。。它应该是ObservaleCollectionIt worked类型,但我没有找到之前不工作的原因:/i用户控件中有4个属性。但其中两人在工作。有了这个,它就开始工作了。。但我很好奇为什么没有出现任何绑定错误。应用与路径=应用没有区别。奇怪的是,我的控制台。WriteLine(..)放在标题getter和setter中时不会启动。嘿,Marc,谢谢你的帮助。这个建议类似于上面的bathineni,用户控件名为root。这是我最终需要做的——但我试图绑定的集合也存在问题。
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();
}
}
}
<Grid >
<my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>