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.

Airn5475
  • 2,452
  • 29
  • 51
Rhaegalex
  • 13
  • 3
  • 3
    AFAIK the target of a binding must be a [property](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties) – MindSwipe Aug 05 '20 at 12:44
  • No values, or all values = 0? – asaf92 Aug 05 '20 at 12:51
  • Does this answer your question? [WPF: simple TextBox data binding](https://stackoverflow.com/questions/1725554/wpf-simple-textbox-data-binding) – Sinatr Aug 05 '20 at 13:51

2 Answers2

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.

Jose Omar
  • 11
  • 3
0

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.

mm8
  • 163,881
  • 10
  • 57
  • 88