简体   繁体   English

WPF数据绑定自定义控件

[英]WPF Databinding a Custom Control

So I've spent about two hours pounding my head against the desk trying everything I can think of to bind to a property on a custom control and none of it works. 因此,我花了大约两个小时的时间将我的头撞在桌子上,尝试将我想到的所有内容绑定到自定义控件上的属性,但没有一个能够正常工作。 If I have something like this: 如果我有这样的事情:

<Grid Name="Form1">
    <mine:SomeControl MyProp="{Binding ElementName=Form1, Path=DataContext.Enable}"/>
    <Button Click="toggleEnabled_Click"/>
</Grid>
public class TestPage : Page
{
    private TestForm _form;

    public TestPage()
    {
        InitializeComponent();
        _form = new TestForm();
        Form1.DataContext = _form;
    }

    public void toggleEnabled_Click(object sender, RoutedEventArgs e)
    {
        _form.Enable = !_form.Enable;
    }
}

TestForm looks like: TestForm看起来像:

public class TestForm
{
    private bool _enable;

    public event PropertyChangedEventHandler PropertyChanged;

    public bool Enable
    {
       get { return _enable; }
       set { _enable = value; OnPropertyChanged("Enable"); }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

And my control looks like: 我的控制看起来像:

<UserControl>
    <TextBox Name="TestBox"/>
</UserControl>
public class SomeControl : UserControl
{
    public static readonly DependencyProperty MyPropProperty =
        DependencyProperty.Register("MyProp", typeof(bool), typeof(SomeControl));

    public bool MyProp
    {
        get { return (bool)GetValue(MyPropProperty); }
        set { SetValue(MyPropProperty, value); }
    }

    public SomeControl()
    {
        InitializeComponent();
        DependencyPropertyDescriptor.FromProperty(MyPropProperty)
            .AddValueChanged(this, Enable);
    }

    public void Enable(object sender, EventArgs e)
    {
        TestBox.IsEnabled = (bool)GetValue(MyPropProperty);
    }
}

Absolutely nothing happens when I click the toggle button. 单击切换按钮时绝对没有任何反应。 If I put a breakpoint inside of the Enable callback it is never hit, whats the deal? 如果我在Enable回调中放置一个断点,它永远不会被击中,这笔交易是什么?

If the Enabled method does not do any more than setting the propertou you could drop it and bind the TextBox.IsEnabled directly: 如果Enabled方法没有做任何设置属性,你可以删除它并直接绑定TextBox.IsEnabled

<UserControl Name="control">
    <TextBox IsEnabled="{Binding MyProp, ElementName=control}"/>
</UserControl>

If you want to keep such a method you should register a property changed callback via UIPropertyMetadata for the dependency property. 如果要保留这样的方法,则应通过UIPropertyMetadata为依赖项属性注册属性更改的回调。


Also this binding is redundant: 此绑定也是多余的:

{Binding ElementName=Form1, Path=DataContext.Enable}

The DataContext is inherited (if you don't set it in the UserControl (which you should never do!)), so you can just use: DataContext是继承的(如果你没有在UserControl设置它(你永远不应该这样做!)),所以你可以使用:

{Binding Enable}

Further if there is trouble with any of the bindings: There are ways to debug them . 此外,如果任何绑定出现问题: 有方法可以调试它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM