在没有viewmodel的情况下从用户控件通知属性更改

时间:2016-09-08 12:37:10

标签: c# wpf

我有一个包装ActiveX控件的UserControl。此控件是一个包含文本和声音的编辑器。 (该文本已被语音识别,因此当您播放声音时,会突出显示正确的单词。)

以下是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 */ }
    }

以下是我尝试使用它的XAML代码:

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

我想在设置TextSound属性时通知SoundLength也已更改。我怎么做?我是否必须为此用户控件实现ViewModel,还是有其他方式?

2 个答案:

答案 0 :(得分:0)

如果我是你,我会创建一个viewModel并将这些值绑定到viewModel中的某些属性。 在将这些值绑定到viewmodel中的属性之前,必须将TextSound和SoundLength属性转换为Dependency属性。(绑定适用于此) 绑定后,您可以编写一些逻辑来设置这些依赖项属性的属性。 为SoundLength设置依赖项属性时,设置另一个属性并通过

发送通知
OnpropertyChanged("SomeProp1"); // prop to SoundLenth
OnpropertyChanged("SomeProp2"); // prop to TextSound

干杯

答案 1 :(得分:-1)

您可以将SoundLength属性定义为DependencyProperty

您的UserControl将如下所示:

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); }
    }
}