English 中文(简体)
WPF 具有约束力的问题
原标题:WPF binding problem

I ve got a problem with binding in XAML/WPF. I created Action class which extends FrameworkElement. Each Action has list of ActionItem. The problem is that the Data/DataContext properties of ActionItem are not set, so they are always null.

XAML:

<my:Action DataContext="{Binding}">
    <my:Action.Items>
        <my:ActionItem DataContext="{Binding}" Data="{Binding}" />
    </my:Action.Items>
</my:Action>

C#:

public class Action : FrameworkElement
{
    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(IList), typeof(Action), 
                                    new PropertyMetadata(null, null), null);

    public Action()
    {
        this.Items = new ArrayList();
        this.DataContextChanged += (s, e) => MessageBox.Show("Action.DataContext");
    }

    public IList Items
    {
        get { return (IList)this.GetValue(ItemsProperty); }
        set { this.SetValue(ItemsProperty, value); }
    }
}

public class ActionItem : FrameworkElement
{
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(ActionItem),
            new PropertyMetadata(
                null, null, (d, v) =>
                {
                    if (v != null)
                        MessageBox.Show("ActionItem.Data is not null");
                    return v;
                }
            ), null
        );

    public object Data
    {
        get { return this.GetValue(DataProperty); }
        set { this.SetValue(DataProperty, value); }
    }

    public ActionItem()
    {
        this.DataContextChanged += (s, e) => MessageBox.Show("ActionItem.DataContext");
    }
}

任何想法?

最佳回答

这些项目不是行动控制的儿童,因此数据目录没有传播。 你们也许没有什么东西可以纠正。

最简单的办法是压倒一切的行动。 采购 改变方法,如果财产=e.DataContext 之后,每个行动项目都分配了E. NewValue。 这是一个最简单但并非非常好的解决方案,因为如果你在项目清单中增加新的行动,那么它就不会有当前的数据背景。

第二,从项目Control中继承行动,并为之提供海关控制模板。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签