C# WPF MediaElement Volume only switches between 0 to 100 and ignores the rest of the slider
I have binded the media element volume and the slider's value and for some reason the volume is either 0 or 100. if the slider is all the way down(value = 0) its muting but if its anything else the volume is 100.
Media Element
<MediaElement x:Name="ME" Stretch="Fill"
Volume="{Binding ElementName=VSlider,Path=Value,
UpdateSourceTrigger=PropertyChanged}" MediaOpened="ME_MediaOpened"/>
Slider's code
<Slider x:Name="VSlider"
Width="30" Height="{x:Static wsm:WindowSettings.wpHeight}" Maximum="100"
Value="{Binding Source={x:Static wsm:WindowSettings.currentVolume }, Mode=OneWay}"
TickFrequency="10" TickPlacement="BottomRight"
SmallChange="1" LargeChange="5" Orientation="Vertical"/>
Because the MediaElement.Volume Property is defined as:
The media's volume represented on a linear scale between 0 and 1. The default is 0.5.
Divide all the settings by 100.
<Slider x:Name="VSlider"
Maximum="1" TickFrequency="0.1" SmallChange="0.01" LargeChange="0.05"
...
/>
Note: these Slider properties are all of type double
.
You can use two properties per setting in the view model. One for display and user entry in a text box and the other for the slider value:
private double _volume; // Range [ 0.0 .. 1.0 ], bind to slider.
public double Volume
{
get => _volume;
set {
if (value != _volume) {
_volume = value;
OnPropertyChanged(nameof(Volume));
OnPropertyChanged(nameof(VolumeDisplay));
}
}
}
// Range [ 0.0 .. 100.0 ], bind to text box.
public double VolumeDisplay
{
get => 100.0 * _volume;
set => Volume = value / 100.0;
}
See also: How to: Implement Property Change Notification.