2012-05-16 19 views
50

टेक्स्टबॉक्स में नए चरित्र टाइप किए जाने पर मैं डेटा बाध्यकारी अपडेट कैसे कर सकता हूं?

मैं डब्ल्यूपीएफ में बाइंडिंग के बारे में सीख रहा हूं और अब मैं एक (उम्मीद है) साधारण मामले पर अटक गया हूं।प्रत्येक नए चरित्र पर एक डब्ल्यूपीएफ टेक्स्टबॉक्स बाध्यकारी आग बनाना?

मेरे पास एक साधारण फ़ाइल लिस्टर क्लास है जहां आप पथ गुण सेट कर सकते हैं, और फिर जब आप फ़ाइल नाम संपत्ति तक पहुंचते हैं तो यह आपको फ़ाइलों की एक सूची देगा।

class FileLister:INotifyPropertyChanged { 
    private string _path = ""; 

    public string Path { 
     get { 
      return _path; 
     } 
     set { 
      if (_path.Equals(value)) return; 
      _path = value; 
      OnPropertyChanged("Path"); 
      OnPropertyChanged("FileNames"); 
     } 
    } 

    public List<String> FileNames { 
     get { 
      return getListing(Path); 
     } 
    } 

    private List<string> getListing(string path) { 
     DirectoryInfo dir = new DirectoryInfo(path); 
     List<string> result = new List<string>(); 
     if (!dir.Exists) return result; 
     foreach (FileInfo fi in dir.GetFiles()) { 
      result.Add(fi.Name); 
     } 
     return result; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string property) { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) { 
      handler(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

मैं इस बहुत ही सरल अनुप्रयोग में एक StaticResource रूप FileLister उपयोग कर रहा हूँ:

<Window x:Class="WpfTest4.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfTest4" 
    Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <local:FileLister x:Key="fileLister" Path="d:\temp" /> 
    </Window.Resources> 
    <Grid> 
     <TextBox Text="{Binding Source={StaticResource fileLister}, Path=Path, Mode=TwoWay}" 
     Height="25" Margin="12,12,12,0" VerticalAlignment="Top" /> 
     <ListBox Margin="12,43,12,12" Name="listBox1" ItemsSource="{Binding Source={StaticResource ResourceKey=fileLister}, Path=FileNames}"/> 
    </Grid> 
</Window> 

बाध्यकारी काम कर रहा है यहाँ कि वर्ग है। यदि मैं टेक्स्टबॉक्स में मान बदलता हूं और फिर इसके बाहर क्लिक करता हूं, तो सूची बॉक्स सामग्री अपडेट हो जाएगी (जब तक पथ मौजूद है)।

समस्या यह है कि जैसे ही कोई नया चरित्र टाइप किया जाता है, मैं अपडेट करना चाहूंगा, और टेक्स्टबॉक्स फोकस होने तक प्रतीक्षा न करें।

मैं यह कैसे कर सकता हूं? क्या यह सीधे xaml में ऐसा करने का कोई तरीका है, या क्या मुझे बॉक्स पर टेक्स्ट चेंज या टेक्स्ट इनपुट ईवेंट को संभालना है?

उत्तर

84

को UpdateSourceTrigger गुण सेट करने के लिए, तुम सब करने की ज़रूरत UpdateSourceTrigger=PropertyChanged सेट कर दिया जाता है।

+1

धन्यवाद! बिल्कुल सरल समाधान के रूप में मैं उम्मीद कर रहा था :) – luddet

+0

मेरे लिए यह काम नहीं किया ... मैं पाठ को अपने पिछले मान पर वापस प्राप्त करना चाहता हूं, अगर यह संख्या नहीं है। केवल जब IsAsync जोड़ा गया = सही यह काम किया। – ilans

20

आप अपने पाठ बॉक्स बंधन में PropertyChanged

<TextBox Text="{Binding Source={StaticResource fileLister}, Path=Path, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
    Height="25" Margin="12,12,12,0" VerticalAlignment="Top" /> 
0

अचानक स्लाइडर और संबंधित टेक्स्टबॉक्स के बीच बाध्यकारी डेटा परेशानियां बना। आखिरकार मुझे कारण मिला और इसे ठीक कर सकता था। कनवर्टर मैं का उपयोग करें:

using System; 
using System.Globalization; 
using System.Windows.Data; 
using System.Threading; 

namespace SiderExampleVerticalV2 
{ 
    internal class FixCulture 
    { 
     internal static System.Globalization.NumberFormatInfo currcult 
       = Thread.CurrentThread.CurrentCulture.NumberFormat; 

     internal static NumberFormatInfo nfi = new NumberFormatInfo() 
     { 
      /*because manual edit properties are not treated right*/ 
      NumberDecimalDigits = 1, 
      NumberDecimalSeparator = currcult.NumberDecimalSeparator, 
      NumberGroupSeparator = currcult.NumberGroupSeparator 
     }; 
    } 

    public class ToOneDecimalConverter : IValueConverter 
    { 
     public object Convert(object value, 
      Type targetType, object parameter, CultureInfo culture) 
     { 
      double w = (double)value; 
      double r = Math.Round(w, 1); 
      string s = r.ToString("N", FixCulture.nfi); 
      return (s as String); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      string s = (string)value; 
      double w; 
      try 
      { 
       w = System.Convert.ToDouble(s, FixCulture.currcult); 
      } 
      catch 
      { 
       return null; 
      } 
      return w; 
     } 
    } 
} 

XAML

<Window.Resources> 
    <local:ToOneDecimalConverter x:Key="ToOneDecimalConverter"/> 
</Window.Resources> 

में आगे परिभाषित पाठ बॉक्स

<TextBox x:Name="TextSlidVolume" 
    Text="{Binding ElementName=SlidVolume, Path=Value, 
     Converter={StaticResource ToOneDecimalConverter},Mode=TwoWay}" 
/> 
संबंधित मुद्दे