简体   繁体   中英

Property Binding to Dependency property in Custom control not working

I have created a custom control with DependencyProperty called “ItemsPerPage”. I have used that custom control in a window. Now if I am assigning value directly to ItemsPerPage property then it is working but if I am binding it with property defined in window then it is not working.

Declaration of Custom Control.

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;
    }
}

Now I have created a New WPF project and in Window1 I have used this custom control as below.

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

Here sg: is a namespace declared to add reference of custom control project.And In .cs file of Window1 I have declared a property.

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));
    }
}

But value 30 is not assigning to ItemsPerPage property. It is showing 0 always. If I am assigning 30 directly ItemsPerPage="30" . Then it is working.

I don't think author of the OP wants to set datacontext to window. You need to bind your control to Window's property like that:

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

ipp should Notify Property Changed:

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();
}

The main problem is, that you did not specify the DataContext of the Window.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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