将DataContext设置为StaticResource的属性

时间:2018-07-17 09:58:13

标签: c# wpf xaml mvvm

我有一个import numpy as np import pertesCVSUI from PyQt5 import QtGui, QtWidgets class PertesCVS(pertesCVSUI.Ui_PertesCVS): def __init__(self, parent=None): # super(PertesCVS, self).__init__(parent) self.setupUi(parent) self.recupVariables() self.btnstate() self.fitenergy() def setupUi(self, parent): super(PertesCVS, self).setupUi(parent) # setup validators self.Eon1_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Eon2_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Eon3_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Ion1_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Ion2_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Ion3_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Eoff1_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Eoff2_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Eoff3_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Ioff1_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Ioff2_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Ioff3_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Vcc_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Imax_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Fd_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Mod_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Rdson_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Dc_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) self.Tf_lineEdit.setValidator(QtGui.QDoubleValidator(0.99, 99.99, 3)) # setup connection self.calculate.clicked.connect(self.recupVariables) self.radioButton1.toggled.connect(self.btnstate) self.radioButton2.toggled.connect(self.btnstate) def btnstate(self): if self.radioButton1.isChecked(): self.Mod_lineEdit.hide() self.label_19.hide() self.Dc_lineEdit.show() self.label_22.show() else: self.Mod_lineEdit.show() self.Dc_lineEdit.hide() self.label_22.hide() self.label_19.show() def recupVariables(self): Eon1 = float((self.Eon1_lineEdit.text())) Eon2 = float((self.Eon2_lineEdit.text())) Eon3 = float((self.Eon3_lineEdit.text())) Ion1 = float((self.Ion1_lineEdit.text())) Ion2 = float((self.Ion2_lineEdit.text())) Ion3 = float((self.Ion3_lineEdit.text())) Eoff1 = float((self.Eoff1_lineEdit.text())) Eoff2 = float((self.Eoff2_lineEdit.text())) Eoff3 = float((self.Eoff3_lineEdit.text())) Ioff1 = float((self.Ioff1_lineEdit.text())) Ioff2 = float((self.Ioff2_lineEdit.text())) Ioff3 = float((self.Ioff3_lineEdit.text())) Imax = (self.Imax_lineEdit.text()) Fd = (self.Fd_lineEdit.text()) def fitenergy(self): ##Energy on Aon = np.array([[1, Ion1, Ion1 * Ion1], [1, Ion2, Ion2 * Ion2], [1, Ion3, Ion3 * Ion3]]) ## the error appears here Aon_inv = np.linalg.inv(Aon) *strong text * Bon = np.array([Eon1, Eon2, Eon3]) xon = np.linalg.solve(Aon_inv, Bon) ##Energy_off Aoff = np.array( [[1, Ioff1, Ioff1 * Ioff1], [1, Ioff2, Ioff2 * Ioff2], [1, Ioff3, Ioff3 * Ioff3]]) Aoff_inv = np.linalg.inv(Aoff) Boff = np.array([Eon1, Eon2, Eon3]) xoff = np.linalg.solve(Aoff_inv, Boff) ### xon_T = np.transpose(xon) xoff_T = np.transpose(xoff) print(xon_T, xoff_T) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) main_widget = QtWidgets.QTabWidget() ui = PertesCVS(main_widget) main_widget.show() sys.exit(app.exec_()) 类来管理顶级组件:

Controller

我有一个public class FooViewModel {} public class Controller { public FooViewModel Foo1ViewModel {get; protected set;} // = new ...; } 的静态资源:

Controller

我希望Windows和UserControls具有<Application.Resources> <local:Controller x:Key="AppController" /> </Application.Resources> 的DataContext属性。

据我所知,是将DataContext设置为Controller本身(这不是我想要的)

Controller

但是我不能将其设置为它的属性

<Window.DataContext>
    <StaticResource ResourceKey="AppController" />
</Window.DataContext>

2 个答案:

答案 0 :(得分:3)

使用Controller实例作为源的绑定:

<Window ...
    DataContext="{Binding Foo1ViewModel, Source={StaticResource AppController}}">

答案 1 :(得分:1)

使用Binding

<Window.DataContext>
    <Binding Path="Foo1ViewModel" Source="{StaticResource AppController}" />
</Window.DataContext>