2014-06-19 38 views
0

這是我的XAML無法獲得更新的觀點,即使模型已經被更新

<Window x:Class="NoteBox.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:NoteBox" 
    Title="NoteBox"> 
<Window.Resources> 
    <local:NoteOctaveConverter x:Key="NoteOctaveConverter"/> 
    <local:DottedNoteConverter x:Key="DottedNoteConverter"/> 
    <local:NoteEnumerationConverter x:Key="NoteEnumerationConverter"/> 
    <local:NoteAccidentalConverter x:Key="NoteAccidentalConverter"/> 
</Window.Resources> 
<Window.InputBindings> 
    <KeyBinding Key="OemPeriod" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="Blank"/> 
    <KeyBinding Key="D0" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="Rest"/> 
    <KeyBinding Key="D1" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N1"/> 
    <KeyBinding Key="D2" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N2"/> 
    <KeyBinding Key="D3" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N3"/> 
    <KeyBinding Key="D4" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N4"/> 
    <KeyBinding Key="D5" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N5"/> 
    <KeyBinding Key="D6" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N6"/> 
    <KeyBinding Key="D7" Command="{Binding KeyboardHotkeyCommand}" CommandParameter="N7"/> 
</Window.InputBindings> 

<Grid x:Name="grid" Background="GreenYellow"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="20"/> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="20"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="20"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="20"/> 
    </Grid.RowDefinitions> 

    <TextBlock Grid.Column="0" Grid.Row="1" 
       Text="{Binding Path=MusicalNotation.Accidental, Converter={StaticResource NoteAccidentalConverter}, Mode=OneWay}" 
       FontSize="15" FontFamily="CourierNew" 
       HorizontalAlignment="Center" VerticalAlignment="Center"/> 
    <TextBlock Grid.Column="1" Grid.Row="1" 
       Text="{Binding Path=MusicalNotation.Note, Converter={StaticResource NoteEnumerationConverter}, Mode=OneWay}" 
       FontSize="15" FontFamily="CourierNew" 
       HorizontalAlignment="Center" VerticalAlignment="Center"/> 
    <ListBox Grid.Column="1" Grid.Row="0" 
      BorderBrush="Transparent" Background="Transparent" 
      ItemsSource="{Binding Path=MusicalNotation.Octave, Converter={StaticResource NoteOctaveConverter}, ConverterParameter=TOP, Mode=OneWay}" 
      HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel IsItemsHost="True"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 
    <ListBox Grid.Column="1" Grid.Row="2" 
      BorderBrush="Transparent" Background="Transparent" 
      ItemsSource="{Binding Path=MusicalNotation.Octave, Converter={StaticResource NoteOctaveConverter}, ConverterParameter=BOT, Mode=OneWay}" 
      HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel IsItemsHost="True"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 
    <ListBox Grid.Column="2" Grid.Row="1" 
      BorderBrush="Transparent" Background="Transparent" 
      ItemsSource="{Binding Path=MusicalNotation.Dot, Converter={StaticResource DottedNoteConverter}, Mode=OneWay}" 
      HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <WrapPanel IsItemsHost="True"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
    </ListBox> 
</Grid> 

這是XAML.CS

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new NoteBoxViewModel(); 
    } 
} 

這是我的ModelView

public class NoteBoxViewModel 
{ 
    public MusicalNotation MusicalNotation { get; set; } 
    public ICommand KeyboardHotkeyCommand { get; private set; } 
    public bool IsSelected { get; set; } 

    public NoteBoxViewModel() 
    { 
     MusicalNotation = new MusicalNotation(); 
     KeyboardHotkeyCommand = new KeyboardHotkeyCommand(this); 
     IsSelected = true; 

     //Initialization testing 
     MusicalNotation.Note = Notes.N1; 
     MusicalNotation.Dot = 1; 
     MusicalNotation.Octave = -2; 
     MusicalNotation.Accidental = Accidentals.Flattened; 
    } 
} 

儘管模型已更新,但視圖不是在使用InputBindings輸入時輸入的。輸入檢測工作正常,模型更新正確。該轉換器也工作得很好,否則,初始化測試不會顯示任何東西。 (來自初始化測試的數據完美顯示)。

我的猜測是在綁定的某個地方。但我找不到任何。

謝謝。

回答

1

你必須實現INotifyPropertyChanged

public class NoteBoxViewModel:INotifyPropertyChanged 
    { 
    //here an example 
     public MusicalNotation MusicalNotation 
    { 
    get{ 
     return _musicalNotation; 
     } 
     set{ _musicalNotation =Value; 
      // by using [CallerMemberName] you don't need to pass the name of the method (this only available in .net 4.5) 
      NotifyPropertyChanged();} 
      }   

     public NoteBoxViewModel() 
     { 
      MusicalNotation = new MusicalNotation(); 
      KeyboardHotkeyCommand = new KeyboardHotkeyCommand(this); 
      IsSelected = true; 

      MusicalNotation.Note = Notes.N1; 
      MusicalNotation.Dot = 1; 
      MusicalNotation.Octave = -2; 
      MusicalNotation.Accidental = Accidentals.Flattened; 


     } 

public event PropertyChangedEventHandler PropertyChanged; 

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
+0

我應該如何編寫'公共事件PropertyChangedEventHandler的PropertyChanged;'這是從'INotifyPropertyChanged'實施?另外,你能否詳細說明'private void NotifyPropertyChanged()'的用途以及它如何使用?謝謝。 –

+0

現在來看看我更新了我的代碼NotifyPropertyChanged()只是調用方法PropertyChanged –

+0

有4個錯誤:** 1。**'NoteBox.NoteBoxViewModel.PropertyChanged'事件只能出現在左側+ =或 - ='和** 2。** NoteBox.NoteBoxViewModel.PropertyChanged':event屬性必須同時添加和移除訪問器和** 3。**'類型或名稱空間名稱'CallerMemberName'可以沒有找到(你是否缺少using指令或程序集引用?)'和** 4。**'無法找到類型或名稱空間名稱'CallerMemberNameAttribute'(你是否缺少using指令或程序集引用?) ' –