1

I am trying to set up a display screen for a voltage source, it has multiple otuput and set values that are all of the same type. I cant get a struct or a class to store these values to work.

Class that stores variables

class DualOutput
{
    private readonly double minVoltage = -1.5;
    private readonly double maxVoltage = 1.5;

    private double setVoltage;
    public double SetVoltage
    {
        get => setVoltage;
        set
        {
            value = value < minVoltage ? minVoltage : value > maxVoltage ? maxVoltage : value;
            setVoltage = value;
        }
    }

    public double Voltage { get; set; }
    public double Current { get; set; }

}

Code that is in the main, dataContext class. [AddINotifyPropertyChangedInterface] is also added to this class

public DualOutput DualOutput1 = new DualOutput()
{
    SetVoltage = 0,
    Voltage = 0,
    Current = 0
};
public DualOutput DualOutput2 = new DualOutput();

The XAML for the portion I am trying to show:

<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding DualOutput1.SetVoltage, StringFormat={}{0:N3} kV}" Style="{StaticResource SetterBox}"/>
<TextBox Grid.Row="0" Grid.Column="2" Text="{Binding DualOutput1.Voltage, StringFormat={}{0:N3} kV}" Style="{StaticResource GetterBox}"/>
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding DualOutput1.Current, StringFormat={}{0:N2} uA}" Style="{StaticResource GetterBox}"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding DualOutput2.SetVoltage, StringFormat={}{0:N3} kV}" Style="{StaticResource SetterBox}"/>
<TextBox Grid.Row="1" Grid.Column="2" Text="{Binding DualOutput2.Voltage, StringFormat={}{0:N3} kV}" Style="{StaticResource GetterBox}"/>
<TextBox Grid.Row="1" Grid.Column="3" Text="{Binding DualOutput2.Current, StringFormat={}{0:N2} uA}" Style="{StaticResource GetterBox}"/>

As you can see, i have tried starting the class both setting and not setting the values.

Am I binding to the values incorrectly? I have tried using both a class and a struct for DualOutput. I don't get error messages, there are just no values displayed.

3
  • 3
    AFAIK the target of a binding must be a property Commented Aug 5, 2020 at 12:44
  • No values, or all values = 0? Commented Aug 5, 2020 at 12:51
  • Does this answer your question? WPF: simple TextBox data binding Commented Aug 5, 2020 at 13:51

2 Answers 2

1

DualOutput1 and DualOutput2 must be public properties for you to be able to bind to them:

public DualOutput DualOutput1 { get; } = new DualOutput()
{
    SetVoltage = 0,
    Voltage = 0,
    Current = 0
};

public DualOutput DualOutput2 { get; } = new DualOutput();

You have defined them as public fields.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it'll work if you create a full property to store the value. Then you raise the OnPropertyChanged event (or whatever you called it) when you set a new value, based on the value the instance of the DualOutput has assigned.

Besides... if you create a class with a setter method, your Voltage property should store its value in a backing field and should not have a set modifier, thus avoiding bypassing the method. Or, your verification logic can go straight into the internal setter of the property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.