all
all

Reputation: 95

Binding error when changing the property to dependency object

i am using the below codes to use a system of class in my control:

public class FlowProWorkingClass:DependencyObject
{
    public TUnitSystemClass ProjectUnitSystem
    {
        get
        {
            return Classes.CurrentFlowProWorkingClass.GeneralOptions.listOfUnitSystems.Find(x => x.Id == Classes.CurrentFlowProWorkingClass.GeneralOptions.DefaultUnitSystemId);
        }
        set
        {
            throw new Exception("Project Unit System can not be setted here!");}
        }
}

and i have bind it to the my control as below:

<WPFTextBoxUnitConverterControl:TextBoxUnitConvertor
    x:Name="txtGasPhaseFlowCoefficient" UnitSystem="{Binding currentFlowProWorkingClass.ProjectUnitSystem, Mode=OneWay, Source={StaticResource CurrentFlowProWorkingClass}, UpdateSourceTrigger=PropertyChanged}"
    Height="27" Margin="167,245,0,0"  VerticalAlignment="Top" HorizontalAlignment="Left" Width="171"/>

this code is working good and get the system of units currently. now need the control , when it is opens, so that i revised the first class to add the below code to notify on change:

public class FlowProWorkingClass:DependencyObject
{
    [JsonIgnore]
    public static readonly DependencyProperty ProjectUnitSystemProperty =
        DependencyProperty.Register(
            "ProjectUnitSystem",
            typeof(TUnitSystemClass),
            typeof(FlowProWorkingClass),
            new PropertyMetadata(ProjectUnitSystemOnChanged));

    private static void ProjectUnitSystemOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }

    public TUnitSystemClass ProjectUnitSystem
    {
        get
        {
            SetValue(ProjectUnitSystemProperty, Classes.CurrentFlowProWorkingClass.GeneralOptions.listOfUnitSystems.Find(x => x.Id == Classes.CurrentFlowProWorkingClass.GeneralOptions.DefaultUnitSystemId));
            return (TUnitSystemClass) GetValue(ProjectUnitSystemProperty);
        }
        set
        {
            throw new Exception("Project Unit System can not be setted here!");
        }
    }
}

but now, the control is not binded at all! whats the problem? NOTE: when i wrote the below code, the binding is ignored fully! the get method is not called at all! why?

public static DependencyProperty ProjectUnitSystemProperty =
    DependencyProperty.Register(
        "ProjectUnitSystem",
        typeof(TUnitSystemClass),
        typeof(FlowProWorkingClass),
        new PropertyMetadata(ProjectUnitSystemOnChanged));

private static void ProjectUnitSystemOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}

public TUnitSystemClass ProjectUnitSystem
{
    get
    {
        SetValue(ProjectUnitSystemProperty, Classes.CurrentFlowProWorkingClass.GeneralOptions.listOfUnitSystems.Find(x => x.Id == Classes.CurrentFlowProWorkingClass.GeneralOptions.DefaultUnitSystemId));
        return (TUnitSystemClass) GetValue(ProjectUnitSystemProperty);
    }
    set
    {
        throw new Exception("Project Unit System can not be setted here!");
    }
}

Upvotes: 0

Views: 68

Answers (1)

Clemens
Clemens

Reputation: 128013

SetValue sets a so-called local value of a dependency property, which replaces any previously assigned (one-way) Binding.

So your property getter effectively removes the Binding. It must not call SetValue.

get
{
    SetValue(ProjectUnitSystemProperty, ...); // remove this line
    return (TUnitSystemClass)GetValue(ProjectUnitSystemProperty);
}

The CLR wrapper of any read/write dependency property must look exactly like this:

public PropertyType PropertyName
{
    get { return (PropertyType)GetValue(PropertyNameProperty); }
    set { SetValue(PropertyNameProperty, value); 
}

For read-only dependency properties take a look at the Read-Only Dependency Properties article on MSDN.

Upvotes: 1

Related Questions