INotifyPropertyChanged没有更新UI(WPF)

时间:2018-04-08 20:07:29

标签: c# wpf mvvm inotifypropertychanged

问题是ViewModel属性与控件属性的绑定无法正常工作。我检查了属性及其值的变化,但控件的可见性不会改变。知道这涉及到什么吗?或者我错过了什么?

视图模型:

<Border Visibility="{Binding MDIPanelVisibility}">
        <Border.InputBindings>
            <MouseBinding MouseAction="LeftClick" Command="{Binding HideMDIPanelCommand}"/>
        </Border.InputBindings>
    </Border>
    <ContentPresenter Width="Auto" Grid.RowSpan="2" Panel.ZIndex="1" VerticalAlignment="Center" Visibility="{Binding MDIPanelVisibility}">
        <ContentPresenter.Content>
            <local:MDIView/>
        </ContentPresenter.Content>
    </ContentPresenter>
    <Button Content="Личный кабинет" FontSize="13" Command="{Binding ShowMDIPanelCommand}">
        <Button.Style>
            <Style TargetType="Button" BasedOn="{StaticResource aLogButton}"/>
        </Button.Style>
    </Button> 

和查看:

public class BingoFrame extends JFrame {

public static final int BINGOSIZE=25;
public static final int BUTTON_X=50;
public static final int BUTTON_Y=50;

public BingoFrame() {
    setResizable(false);
    String[] bingoField = null;
    BingoButton[] buttons=new BingoButton[25];

try {
  bingoField = Utils.getRandomBingoField("Test");
} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

this.setTitle("BS Bingo");
this.setResizable(false);
this.setLocation(50, 50);
this.setSize(600, 800);
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

JPanel buttonPanel = new JPanel();
buttonPanel.setBounds(0, 0, 594, 772);
getContentPane().add(buttonPanel);
buttonPanel.setLayout(null);

for(int i=0;i<BINGOSIZE;i++) {
  buttons[i] = new BingoButton("Text");
}

//decorate buttons and add an action listener
for(int i=0;i<BINGOSIZE;i++) {
  final BingoButton temp = buttons[i];
  temp.setText(bingoField[i]);
  temp.setBackground(Color.white);
  temp.setForeground(Color.blue);
  temp.setPreferredSize(new Dimension(BUTTON_X,BUTTON_Y));

  temp.addActionListener(new ActionListener() {
    boolean toggle = false;
    @Override
    public void actionPerformed(ActionEvent e) {
      if (!temp.isSet()) {
        temp.setBackground(Color.blue);
        temp.setForeground(Color.white);
      } else {
        temp.setBackground(Color.white);
        temp.setForeground(Color.blue);
      }
      temp.toggle();
    }
  });

  buttons[i]=temp;
} 

//set Location for the buttons
for(int i=0;i<5;i++) {
    buttons[i].setLocation(100,(50*i)+10*(i+1));
}    
for(int i=5;i<10;i++) {
    buttons[i].setLocation(160,(50*i)+10*(i+1));
}
for(int i=10;i<15;i++) {
    buttons[i].setLocation(220,(50*i)+10*(i+1));
}
for(int i=15;i<20;i++) {
    buttons[i].setLocation(280,(50*i)+10*(i+1));
}
for(int i=20;i<25;i++) {
    buttons[i].setLocation(340,(50*i)+10*(i+1));
}

//add buttons to the panel
for(int i=0;i<BINGOSIZE;i++) {
  buttonPanel.add(buttons[i]);
}    

this.setVisible(true);   

1 个答案:

答案 0 :(得分:5)

MainViewModel类需要从您的类没有的INotifyPropertyChanged继承,以便绑定框架在设置视图DataContext时按预期运行到MainViewModel实例。

更新课程定义

public class MainViewModel: INotifyPropertyChanged {
    //...
}