自定义控件中的属性绑定到依赖项属性不起作用

时间:2018-10-02 06:43:28

标签: c# wpf data-binding dependency-properties

我用DependencyProperty创建了一个名为“ ItemsPerPage”的自定义控件。我已经在窗口中使用了该自定义控件。现在,如果我直接将值分配给ItemsPerPage属性,那么它将起作用,但是如果我将其与window中定义的属性绑定,则它将无法起作用。

自定义控件的声明。

public class SmartDataGrid : DataGrid
{

    public SmartDataGrid()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(SmartDataGrid), new FrameworkPropertyMetadata(typeof(SmartDataGrid)));
    }


    public static readonly DependencyProperty ItemsPerPageProperty =
       DependencyProperty.Register("ItemsPerPage", typeof(Int32),
           typeof(SmartDataGrid)
           , new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnItemsPerPageChanged, CoerceTextProperty, true, UpdateSourceTrigger.PropertyChanged));

    public Int32 ItemsPerPage
    {
        get
        {
            return (Int32)GetValue(ItemsPerPageProperty);
        }
        set
        {
            SetValue(ItemsPerPageProperty, value);
        }
    }

    private static void OnItemsPerPageChanged(DependencyObject defectImageControl, DependencyPropertyChangedEventArgs eventArgs)
    {
        var control = (SmartDataGrid)defectImageControl;
        control.callmyInstanceMethod(eventArgs);
    }

    private static object CoerceTextProperty(DependencyObject d, object value)
    {
        return value ?? 0;
    }
    private void callmyInstanceMethod(DependencyPropertyChangedEventArgs e)
    {
        ItemsPerPage = (Int32)e.NewValue;
    }
}

现在,我创建了一个新的WPF项目,并在Window1中使用了以下自定义控件。

<Grid>
        <sg:SmartDataGrid ItemsPerPage="{Binding ipp}"></sg:SmartDataGrid>
    </Grid>

这里sg:是声明为添加自定义控件项目的引用的命名空间。在Window1的.cs文件中,我声明了一个属性。

public partial class Window1 : Window, INotifyPropertyChanged
{

    int _ipp;
    public int ipp
    { get { return _ipp; } set { _ipp = value; RaisePropertyChanged(); } }

    public Window1()
    {
        ipp = 30;
        InitializeComponent();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

但是值30未分配给ItemsPerPage属性。总是显示0。 如果我直接分配30 ItemsPerPage="30"。然后就可以了。

2 个答案:

答案 0 :(得分:1)

我认为OP的作者不想将datacontext设置为window。 您需要像这样将控件绑定到Window的属性:

ItemsPerPage="{Binding Path=ipp,
                          RelativeSource={RelativeSource Mode=FindAncestor,
                                                         AncestorType=Window}}"

ipp应该通知属性已更改:

int _ipp;
public int ipp
{ 
   get=>_ipp;
   set{
        if (value!=_ipp)
        {
           _ipp=value;
           RaisePropertyChanged(nameof(ipp));
        }
   }
}
void RaisePropertyChanged(string propname)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
}


public Window1()
{
     ipp = 30;
     InitializeComponent();
}

答案 1 :(得分:1)

主要问题是,您没有指定窗口的DataContext

public partial class Window1 : Window
{
    public int ipp { get; set; }
    public Window1()
    {
        ipp = 30;
        InitializeComponent();
        DataContext = this;
    }
}
相关问题