<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:myapp="clr-namespace:MyPlayer.Model"
mc:Ignorable="d"
x:Class="MyPlayer.VolumeButtons"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">
<UserControl.Resources>
<myapp:MusicPlayerModel x:Key="Model"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" DataContext="{StaticResource Model}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="35px"/>
<ColumnDefinition Width="1.0*"/>
</Grid.ColumnDefinitions>
<Slider Value="{Binding Volume}" Margin="0,0,0,0" Grid.Column="1" VerticalAlignment="Center" x:Name="volumeSlider"/>
<Button Margin="4,4,4,4" Content="Button" x:Name="muteButton" Click="MuteButton_Click"/>
</Grid>
</UserControl>
现在的问题是,当我移动滑块时数据绑定工作正常(当我移动滑块时模型会更新)。
但是,当单击按钮时,我会更改模型中的值并期望它更新视图。
下面的代码:
private void MuteButton_Click(object sender, RoutedEventArgs e)
{
musicPlayerModel.Volume = 0;
}
模型中的代码:
public double Volume
{
get { return this.volume; }
set
{
this.volume = value;
this.OnPropertyChanged("SomeTestText");
this.OnPropertyChanged("Volume");
}
}
但在 OnPropertyChanged 中,该事件为空,因此没有任何反应。为什么事件为空而不是当我的滑块移动时以及如何解决它?