Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The reason this is not working is that the property change callback isn't being raised (as the bound value from your VM is the same as the default value specified in the metadata for the property). More fundamentally though, your behavior will detach when the selected text is set to null. In cases like this, I tend to have another attached property that is simply used to enable the monitoring of the selected text, and then the SelectedText property can be bound. So, something like so:</p> <pre><code>#region IsSelectionMonitored public static readonly DependencyProperty IsSelectionMonitoredProperty = DependencyProperty.RegisterAttached( "IsSelectionMonitored", typeof(bool), typeof(PinnedInstrumentsViewModel), new FrameworkPropertyMetadata(OnIsSelectionMonitoredChanged)); [AttachedPropertyBrowsableForType(typeof(TextBox))] public static bool GetIsSelectionMonitored(TextBox d) { return (bool)d.GetValue(IsSelectionMonitoredProperty); } public static void SetIsSelectionMonitored(TextBox d, bool value) { d.SetValue(IsSelectionMonitoredProperty, value); } private static void OnIsSelectionMonitoredChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { TextBox tb = obj as TextBox; if (tb != null) { if ((bool)e.NewValue) { tb.SelectionChanged += tb_SelectionChanged; } else { tb.SelectionChanged -= tb_SelectionChanged; } SetSelectedText(tb, tb.SelectedText); } } #endregion #region "Selected Text" public static string GetSelectedText(DependencyObject obj) { return (string)obj.GetValue(SelectedTextProperty); } public static void SetSelectedText(DependencyObject obj, string value) { obj.SetValue(SelectedTextProperty, value); } // Using a DependencyProperty as the backing store for SelectedText. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.RegisterAttached( "SelectedText", typeof(string), typeof(TextBoxHelper), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, SelectedTextChanged)); private static void SelectedTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { TextBox tb = obj as TextBox; if (tb != null) { tb.SelectedText = e.NewValue as string; } } static void tb_SelectionChanged(object sender, RoutedEventArgs e) { TextBox tb = sender as TextBox; if (tb != null) { SetSelectedText(tb, tb.SelectedText); } } #endregion </code></pre> <p>And then in your XAML, you'd have to add that property to your first TextBox:</p> <pre><code>&lt;TextBox ... local:TextBoxHelper.IsSelectionMonitored="True" local:TextBoxHelper.SelectedText="{Binding SelectedText, Mode=OneWayToSource}" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload