2009-07-24 13 views

उत्तर

0

जहां तक ​​मुझे पता है, इस सुविधा को सिल्वरलाइट 2.0 में शामिल नहीं किया गया है।

this कार्य-आसपास के समाधान के लिए आलेख पढ़ें।

+0

यह सुविधा Silverlight 3. में जोड़ा गया था और इस उदाहरण में बहुत अच्छी तरह से काम करता है: <स्लाइडर एक्स: नाम = "HeightSetterSlider" अधिकतम = "200" मूल्य = "100" /> लेकिन मेरा काम नहीं करता है। मैं उस लेख को नापसंद करता हूं, यह शैली वास्तव में बाध्यकारी की सभी सुंदरता को मार देती है। – Ivan

1

आप चयन स्टार्ट से जुड़ नहीं सकते क्योंकि यह निर्भरता प्रॉपर्टी नहीं है।

+2

:) बहुत बहुत धन्यवाद। – Ivan

+0

क्या यह पता लगाने का कोई तरीका है कि किसी दिए गए नियंत्रण पर कौन से गुण निर्भरताप्रॉपर्टीज हैं और कौन नहीं हैं? –

+0

सबसे तेज़ तरीका विजुअल स्टूडियो के इंटेलिजेंस का उपयोग करना है। उदाहरण के लिए, मान लें कि आप टेक्स्टबॉक्स की सभी निर्भरता गुण देखना चाहते हैं। बस टेक्स्टबॉक्स टाइप करें। और इंटेलिजेंस आपको इसकी सभी निर्भरता गुण दिखाएगा। –

9

मैं इस समस्या (selectionstart और SelectionLength निर्भरता गुण नहीं हैं) में पड़ गए और bindable चयन आरंभ और अंत के साथ एक पाठ बॉक्स बनाने का फैसला किया:

दृश्य:

public class SelectionBindingTextBox : TextBox 
{ 
    public static readonly DependencyProperty BindableSelectionStartProperty = 
     DependencyProperty.Register(
     "BindableSelectionStart", 
     typeof(int), 
     typeof(SelectionBindingTextBox), 
     new PropertyMetadata(OnBindableSelectionStartChanged)); 

    public static readonly DependencyProperty BindableSelectionLengthProperty = 
     DependencyProperty.Register(
     "BindableSelectionLength", 
     typeof(int), 
     typeof(SelectionBindingTextBox), 
     new PropertyMetadata(OnBindableSelectionLengthChanged)); 

    private bool changeFromUI; 

    public SelectionBindingTextBox() : base() 
    { 
     this.SelectionChanged += this.OnSelectionChanged; 
    } 

    public int BindableSelectionStart 
    { 
     get 
     { 
      return (int)this.GetValue(BindableSelectionStartProperty); 
     } 

     set 
     { 
      this.SetValue(BindableSelectionStartProperty, value); 
     } 
    } 

    public int BindableSelectionLength 
    { 
     get 
     { 
      return (int)this.GetValue(BindableSelectionLengthProperty); 
     } 

     set 
     { 
      this.SetValue(BindableSelectionLengthProperty, value); 
     } 
    } 

    private static void OnBindableSelectionStartChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as SelectionBindingTextBox; 

     if (!textBox.changeFromUI) 
     { 
      int newValue = (int)args.NewValue; 
      textBox.SelectionStart = newValue; 
     } 
     else 
     { 
      textBox.changeFromUI = false; 
     } 
    } 

    private static void OnBindableSelectionLengthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as SelectionBindingTextBox; 

     if (!textBox.changeFromUI) 
     { 
      int newValue = (int)args.NewValue; 
      textBox.SelectionLength = newValue; 
     } 
     else 
     { 
      textBox.changeFromUI = false; 
     } 
    } 

    private void OnSelectionChanged(object sender, RoutedEventArgs e) 
    { 
     if (this.BindableSelectionStart != this.SelectionStart) 
     { 
      this.changeFromUI = true; 
      this.BindableSelectionStart = this.SelectionStart; 
     } 

     if (this.BindableSelectionLength != this.SelectionLength) 
     { 
      this.changeFromUI = true; 
      this.BindableSelectionLength = this.SelectionLength; 
     } 
    } 
} 
+0

अच्छा आदमी, बहुत उपयोगी! –

0

यह एक वैकल्पिक समाधान हो सकता है :

<TextBox Text="{Binding Text}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="SelectionChanged"> 
      <mvvml:EventToCommand Command="{Binding TextBoxSelectionChangedCommand}" 
           PassEventArgsToCommand="True" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</TextBox> 

ViewModel:

#region TextBoxSelectionChangedCommand 
    RelayCommand<RoutedEventArgs> _TextBoxSelectionChangedCommand = null; 
    public ICommand TextBoxSelectionChangedCommand { 
     get { 
      if (_TextBoxSelectionChangedCommand == null) { 
       _TextBoxSelectionChangedCommand = new RelayCommand<RoutedEventArgs>((r) => TextBoxSelectionChanged(r), (r) => true); 
      } 

      return _TextBoxSelectionChangedCommand; 
     } 
    } 

    protected virtual void TextBoxSelectionChanged(RoutedEventArgs _args) { 
     YourCursorPositionVariable = (_args.OriginalSource as System.Windows.Controls.TextBox).SelectionStart; 
    } 
    #endregion 

मैं मानता हूं कि आपको व्यूमोडेल में टेक्स्टबॉक्स घटक प्रकार डालना है और यह एक प्रकार का युग्मन है, लेकिन एक कस्टम घटक भी एक विशिष्ट संपत्ति पर बाध्य करने के लिए लगाएगा।

संबंधित मुद्दे