[英]User Control DataContext/Binding Issue with Dependency Property WPF
[英]Binding to a dependency property of a user control WPF/XAML
我的应用程序如下所示:
SectionHeader
SectionHeader
内容
SectionHeader
内容
SectionHeader
是一个用户控件,具有两个依赖属性 = Title 和 Apps。
标题不变,但应用程序需要绑定到主 window 视图 model 应用程序属性。 只有三个部分标题中的两个需要 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}">
所以我的问题是:
编辑:
忘了说 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;
}
为您的 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 属性的用户控件的元素绑定
尝试从您的描述中重现这一点并遇到类似问题后,我发现我可以使它工作的唯一方法是不设置 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();
}
}
}
就像调用它的 XAML 一样:
<Grid >
<my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>
(我将您的用户控件中的Apps
名称更改为UCApps
,这样我就可以看到发生了什么 - 太多同名的属性会让人感到困惑!)
Apps="{Binding Path=Apps}"
将应用程序绑定到自身。
尝试关注
1)您的ViewModel应该是Control的DataContext,然后您可以使用以下
Apps="{Binding DataContext.Apps}"
您还可以使用Snoop实用程序实时查找绑定问题
你有没有尝试过
Apps="{Binding Apps}"
代替
Apps="{Binding Path=Apps}"
在您的顶级控制中?
另外,当您运行应用程序时,您是否在 VS output window 中遇到任何绑定错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.