0

I have a UserControl that wraps an ActiveX control. This control is an editor that has both text and sound. (The text has been speech-recognized so that when you play the sound the correct word is highlighted).

Here is the code from the UserControl:

public partial class EditorWrapper : UserControl
{
    private CdsEditorOverrides editorCtrl;

    public TextWithSound TextSound
    {
        set
        {
            try
            {
                if(value.Text != null && value.Stream != null)
                {
                    editorCtrl.LoadDocumentSetAsXmlString(value.Text);
                    editorCtrl.GetAudioPlayer().LoadAudioFromStream(new StreamWrapper(value.Stream));
                    editorCtrl.GetAudioPlayer().AudioStreamingComplete();
                    Debug.WriteLine("TextSound updated");
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error loading Text: " + ex.ToString());
                //don't throw in get/set
            }
        }
    }

    public int SoundLength
    {
        get
        {
            return editorCtrl.GetAudioPlayer().GetPlaybackLength();
        }
        set { /* do nothing */ }
    }

Here is the XAML code where I try to use it:

<editorWrapper:EditorWrapper 
    Name="editorObj" 
    TextSound="{Binding Dictation.TextWithSound}"
    SoundLength="{Binding Dictation.SoundLengthInMilliseconds, Mode=OneWayToSource}"/>

I want to notify when the TextSound property is set that the SoundLength is also changed. How do I do that? Do I have to implement a ViewModel for this user-control or is there another way?

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Lucian
  • 3,407
  • 2
  • 21
  • 18

2 Answers2

0

If I were you I would create a viewModel and bind those values to some properties in the viewModel. Before bind those values to properties in viewmodel, you have to convert TextSound and SoundLength properties to Dependency properties.(Binding works on that) After binding, you may write a little logic in order to set the properties for those dependency properties. When you set the dependency property for SoundLength, set the other one and send notification via

OnpropertyChanged("SomeProp1"); // prop to SoundLenth
OnpropertyChanged("SomeProp2"); // prop to TextSound

Cheers

FreeMan
  • 1,417
  • 14
  • 20
-1

You could define the SoundLength property as a DependencyProperty.

Your UserControl would look like this:

public partial class EditorWrapper : UserControl
{
    // Your defined DependencyProperty
    public static readonly DependencyProperty SoundLengthProperty=DependencyProperty.Register("SoundLength",typeof(int),typeof(EditorWrapper),new PropertyMetadata(OnSoundLengthChanged));

    public TextWithSound TextSound
    {
        set
        {
            try
            {
                if(value.Text != null && value.Stream != null)
                {
                    CdsEditorOverrides editorCtrl.LoadDocumentSetAsXmlString(value.Text);
                    editorCtrl.GetAudioPlayer().LoadAudioFromStream(new StreamWrapper(value.Stream));
                    editorCtrl.GetAudioPlayer().AudioStreamingComplete();

                    // set the sound length (will automatically notify the binding)
                    SoundLength=editorCtrl.GetAudioPlayer().GetPlaybackLength();

                    Debug.WriteLine("TextSound updated");
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error loading Text: " + ex.ToString());
                //don't throw in get/set
            }
        }
    }

    // The actual Property: gets/sets the value to the DependencyProperty
    public int SoundLength
    {
        get { return (int)GetValue(SoundLengthProperty); }
        private set { SetValue(SoundLengthProperty,value); }
    }
}
GregorMohorko
  • 2,739
  • 2
  • 22
  • 33
  • I used this solution because I already had DependencyProperty for other properties. But I changed so that the code is: editorCtrl.GetAudioPlayer().AudioStreamingComplete(); //SoundLength = editorCtrl.GetAudioPlayer().GetPlaybackLength(); SetValue(SoundLengthDep, editorCtrl.GetAudioPlayer().GetPlaybackLength()); – Lucian Sep 08 '16 at 15:10
  • Thank you for letting me know. – GregorMohorko Sep 08 '16 at 19:34