即使模型已更新,也无法更新视图

时间:2014-06-19 15:14:10

标签: c# wpf xaml data-binding mvvm

这是我的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 个答案:

答案 0 :(得分: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));
            }
        }
    }