如果包含元素具有相同名称的属性,如何绑定WPF

时间:2014-11-05 09:10:20

标签: c# wpf xaml data-binding user-controls

我有以下用户控件XAML:

<UserControl x:Class="WpfEditorTest.PipelineView"
         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"
         xmlns:local="clr-namespace:WpfEditorTest"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<ItemsControl ItemsSource="{Binding Processors}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:EditorWindow WindowResize="EditorWindow_WindowResize">
                <StackPanel>
                    <Label Content="{Binding Name}"/>
                    <Label Content="{Binding X}"/>
                    <Label Content="{Binding Y}"/>
                </StackPanel>
            </local:EditorWindow>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>

usercontrol的DataContext(在构造函数中设置):

DataContext = new Pipeline("Pipeline1")
{
    Processors = 
    {
        new Processor("Add1"),
        new Processor("Add2"),
    }
};

二手(视图)模型的最小版本:

public class Pipeline : Processor
{
    private ObservableCollection<Processor> m_Processors = new ObservableCollection<Processor>();

    public ObservableCollection<Processor> Processors
    {
        get
        {
            return m_Processors;
        }
    }
    public Pipeline(string name) : base(name)
    {
    }
}

public class Processor : NotifierBase
{
    private string m_Name;
    public string Name
    {
        get { return m_Name; }
        set { m_Name = value; OnPropertyChanged("Name"); }
    }
    private double m_X;
    public double X
    {
        get { return m_X; }
        set { m_X = value; OnPropertyChanged("X"); }
    }
    private double m_Y;
    public double Y
    {
        get { return m_Y; }
        set { m_Y = value; OnPropertyChanged("Y"); }
    }
    public Processor(string name)
    {
        Name = name;
    }
}

public abstract class NotifierBase : INotifyPropertyChanged
{
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion INotifyPropertyChanged Members
}

在这种情况下,我的DataContext(usercontrol本身)是一个具有ObservableCollection&lt;&gt;的对象。处理器,每个处理器都有Name,X和Y属性。 EditorWindow还具有Name,X和Y属性。我如何制作它,以便stackpanel中的标签显示usercontrol的DataContext的值。

结果:

Result of code

如果它说'EditorWindowControl'它应该说'Add1'或'Add2',当前文本是EditorWindow实例的Name属性,而不是来自datacontext(处理器)集合中的当前项

感谢您阅读我的问题

修改

我将namename从Name更改为ProcessorName,并将绑定设置为“{Binding ProcessorName}”,但现在绑定失败(label为空)。所以这意味着它绑定的方式是错误的,但我仍然不知道它出错的地方。

1 个答案:

答案 0 :(得分:0)

错误发生在EditorWindow控件中,在构造函数中它将DataContext设置为自身,从而覆盖了所有其他绑定(显然)。移除后,数据绑定工作。

感谢@dkozl指出这个