[英]WPF binding to variable of another class or struct
我正在嘗試為電壓源設置顯示屏,它具有多個相同類型的輸出和設置值。 我無法獲得結構或 class 來存儲這些值以使其正常工作。
Class 存儲變量
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; }
}
主要的代碼,dataContext class。 [AddINotifyPropertyChangedInterface]
也被添加到這個 class
public DualOutput DualOutput1 = new DualOutput()
{
SetVoltage = 0,
Voltage = 0,
Current = 0
};
public DualOutput DualOutput2 = new DualOutput();
我要展示的部分的 XAML :
<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}"/>
如您所見,我嘗試啟動 class 設置和不設置值。
我是否錯誤地綁定到這些值? 我嘗試使用 class 和DualOutput
結構。 我沒有收到錯誤消息,只是沒有顯示任何值。
我認為如果您創建一個完整的屬性來存儲值,它會起作用。 然后,根據DualOutput實例分配的值,在設置新值時引發OnPropertyChanged事件(或任何你稱之為的事件)。
此外...如果您使用 setter 方法創建 class,您的Voltage屬性應將其值存儲在支持字段中並且不應具有set修飾符,從而避免繞過該方法。 或者,您的驗證邏輯可以 go 直接進入屬性的內部設置器。
DualOutput1
和DualOutput2
必須是公共屬性才能綁定到它們:
public DualOutput DualOutput1 { get; } = new DualOutput()
{
SetVoltage = 0,
Voltage = 0,
Current = 0
};
public DualOutput DualOutput2 { get; } = new DualOutput();
您已將它們定義為公共字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.