0

I have a custom media player object that I create in code behind from a user control. There can be 1 to 4 of these at any time, but i want to bind the volume and the mute property of only one to a xaml control EG.

The control is:

MediaControlplayer  vcMediaPlayerMaster = new MediaControlplayer();

In this case the mute option to the ischecked state of the control does not work. How can I hook the binding up to the properties of the control when it is instantiated in code behind ?

xaml is like this. The variable vcMediaPlayerMaster is a global variable in code behind. When i instantiated it i assumed its declaration as a global predefined variable would allow the xaml below to bind to it, but it seems not to be the case.

<ToggleButton x:Name="btnAudioToggle" ToolTip="Audio Mute/Unmute"  
Click="BtnAudioToggle_OnClick" IsChecked="{Binding Mode =TwoWay, 
ElementName=vcMediaPlayerMaster, Path=Mute}" BorderBrush="LightBlue" 
Width="32" Height="32" Margin="0,5,10,10" Background="{StaticResource 
IbAudio}" Style="{DynamicResource ToggleButtonStyle1}" > </ToggleButton>

I thought perhaps creating a binding in code behind may be the way to go, but i cant seem to find a simple example that explains the code behind process to do that to fit my case.

Manity
  • 21
  • 1
  • 6

2 Answers2

0

You could create a helper class to hold the currently active MediaPlayer.
As a simple example:

public class MediaPlayerHelper : INotifyPropertyChanged
{
    private MediaControlplayer currentPlayer;

    public static MediaPlayerHelper Instance { get; } = new MediaPlayerHelper();

    public MediaControlplayer CurrentPlayer 
    { 
        get => this.currentPlayer;
        set { /* Implement a setter with INotifyPropertyChanged */ }
    }

    // Implement INotifyPropertyChanged here
}

The binding to this would look like the following

<Slider Value="{Binding Volume, Source={x:Static helper:MediaPlayerHelper.Instance}}"/>

Don't forget to include the namespace in the opening tag of your class in XAML:

xmlns:helper="clr-namespace:SomeNamespace.Helper"

Now you just have to change the currently used MediaPlayer whenever it changes:

MediaPlayerHelper.Instance.CurrentPlayer = newCurrentPlayer;
IDarkCoder
  • 709
  • 7
  • 18
  • I guess this is a possibility, but i was thinking there is a way directly in code behind to be able to bind to the xaml control, which i think may be quicker and cleaner. – Manity Feb 18 '19 at 17:01
  • What do you mean by directly binding the xaml control? – IDarkCoder Feb 18 '19 at 17:29
  • I mean creating a new binding in code behind such as Binding myBinding = new Binding(); but none of the examples ive tried seem to work. I also just assumed that creating it in code behind after the player object was instantiated would allow one to hook up the binding directly that way as opposed to using this helper class method. – Manity Feb 19 '19 at 07:49
  • Binding it like that in code behind should work. Maybe add the code you used for the binding to the question to check what you did wrong or open a new question for it. IMHO binding to a helper like that is cleaner than changing the binding in code behind though. – IDarkCoder Feb 19 '19 at 08:26
0

Ok i finally got it to work. Applied the binding in code behind fully. I was able to bind the property i wanted to the ischecked property of a button to toggle the bool property of the mediaplayer object

 MediaControlplayer  vcMediaPlayerMaster = new MediaControlplayer();
 Binding myMuteBinding = new Binding("Mute");
 myMuteBinding.Source = vcMediaPlayerMaster;
 myMuteBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
 myMuteBinding.Mode = BindingMode.TwoWay;
 btnAudioToggle.SetBinding(SimpleButton.IsCheckedProperty, myMuteBinding);

So this worked fine for me and i used the same principle to bind other properties.

Manity
  • 21
  • 1
  • 6