我的应用程序看起来是这样的:
SectionHeader
SectionHeader
内容
SectionHeader
内容
SectionHeader是一个具有两个依赖项属性= Title和Apps的用户控件。
标题不会更改,但是应用程序需要绑定到主窗口视图模型Apps属性。仅对三个区段标题中的两个使用Apps属性。
<c:SectionHeader DockPanel.Dock="Top" x:Name="SectionResources" Title="RESOURCES"
Apps="{Binding Path=Apps}" />目前的情况就是这样。问题是应用程序没有出现。
在SectionHeader中,DataContext设置为自己,如下所示,允许显示标题。
DataContext="{Binding RelativeSource={RelativeSource Self}}"应用程序是ItemsSource,用于UserControl中的ItemsControl:
<ItemsControl
ItemsSource="{Binding Apps}">所以我的问题是:
编辑:
忘了提到Apps是ObservableCollection of AppsItems。
这就是我的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;
}发布于 2011-07-22 09:29:50
给您的usecontrol命名,并尝试像这样绑定
ItemsSource="{Binding Apps,ElementName=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属性的用户控件的元素。
发布于 2011-07-22 14:16:29
在尝试从您的描述中复制这一点之后,遇到了类似的问题,我发现使其工作的唯一方法是不设置UserControl的UserControl,而是使用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的XAML一样:
<Grid >
<my:UserControl1 x:Name="ucName" UCApps="{Binding Path=Apps}"/>
</Grid>(我将用户控件中的Apps更改为UCApps,这样我就可以看到发生了什么--太多相同名称的属性使人感到困惑!)
发布于 2011-07-22 08:13:20
Apps="{Binding Path=Apps}"将应用程序绑定到自己身上。
试着跟随
1)您的ViewModel应该是控制的DataContext,然后您可以使用以下方法
Apps="{Binding DataContext.Apps}"此外,还可以使用斯诺普实用程序实时查找绑定问题。
https://stackoverflow.com/questions/6787219
复制相似问题