簡體   English   中英

即使模型已經更新,也無法更新視圖

[英]Can't get the view updated, even though the model is already updated

這是我的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輸入時視圖也不會。 輸入檢測正常運行,並且模型正確更新。 轉換器也可以很好地工作,否則,初始化測試也不會顯示任何內容。 (來自初始化測試的數據顯示完美)。

我的猜測在綁定上。 但是我找不到。

謝謝。

注意

您必須實現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));
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM