我的应用程序如下所示:
SectionHeader
SectionHeader
内容
SectionHeader
内容
SectionHeader 是具有两个依赖项属性=标题和应用程序的用户控件.
标题不会更改,但应用程序需要绑定到主窗口视图模型Apps属性.只有三个部分标题中的两个需要Apps属性.
<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
Apps="{Binding Path=Apps}" />
Run Code Online (Sandbox Code Playgroud)
这就是目前的情况.问题是应用程序没有出现.
在SectionHeaderDataContext中设置为自身如下,允许标题显示.
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Run Code Online (Sandbox Code Playgroud)
应用程序是ItemsControlUserControl中a的ItemsSource :
<ItemsControl
ItemsSource="{Binding Apps}">
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
编辑:
忘了提到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;
}
Run Code Online (Sandbox Code Playgroud)
Bat*_*eni 21
给你的usecontrol命名并尝试像这样绑定
ItemsSource="{Binding Apps,ElementName=root}"
Run Code Online (Sandbox Code Playgroud)
和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">
Run Code Online (Sandbox Code Playgroud)
它是具有Apps属性的用户控件的元素绑定
Mar*_*rcE 10
试图从你的描述中重新编写这个并遇到类似的问题后,我发现我能使其工作的唯一方法是不设置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>
Run Code Online (Sandbox Code Playgroud)
这个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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
就像调用它的XAML一样:
<Grid >
<my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
(我Apps在你的Usercontrol中更改了名称,UCApps以便我可以看到发生了什么 - 太多同名的属性令人困惑!)