2015-08-05 2 views
5

मैं डब्ल्यूपीएफ प्रोग्रामिंग के लिए नया हूं और सी # कोडिंग में पेशेवर नहीं हूं।कक्षाओं के साथ शब्दकोश के लिए डब्ल्यूपीएफ बाध्यकारी

तो मुझे निम्न समस्या है: मेरे पास कई अलग-अलग comboboxes और टेक्स्टबॉक्स हैं। मैं उन्हें पृष्ठभूमि में एक चर के साथ बांधना चाहता हूं, क्योंकि मैं डेटाबेस से डेटा के साथ comboboxes को भरना चाहता हूं।

private Dictionary<string, Filter> _ctrlFilter; 

public Dictionary<string, Filter> ctrlFilter { 
    get { return _ctrlFilter; } 
    set { _ctrlFilter = value; } 
} 

जब मैं liste.xaml लोड यह निम्न कोड के साथ ctrlFilter शब्दकोश initializes:

तो मैं
liste.xaml.cs फ़ाइल में निम्न बनाया

ctrlFilter = new Dictionary<string, Filter> 
{ 
    //ComboBox 
    {"hersteller", new Filter("Hersteller", typeof(ComboBox)) }, 
    {"fahrzeugart", new Filter("Fahrzeugart", typeof(ComboBox), false) }, 

    //TextBox 
    {"baujahr", new Filter("Baujahr", typeof(TextBox), true, typeof(short)) }, 
    {"anschaffungswert", new Filter("Anschaffungswert", typeof(TextBox), true, typeof(decimal)) }, 
    {"finanz_restwert", new Filter("Restwert", typeof(TextBox), true, typeof(decimal)) }, 

    //Others 
    {"zugelassen", new Filter("Zugelassen", typeof(DatePicker), true, typeof(DateTime)) }, 
    {"vstabzug", new Filter("Vorsteuerabzug", typeof(CheckBox), true, typeof(bool)) }, 
}; 

तो फ़िल्टर श्रेणी (जो liste.xaml.cs में भी है) इस तरह दिखती है:

public class Filter 
{ 
    public string FilterName; 
    public Type ControlType; 
    public Type FromToType; 
    public bool Load; 
    public ObservableCollection<object> Items; 
    public object SelectedFilter; 
    public object From; 
    public object To; 

    public Filter(string name, Type type, bool load = true, Type ftType = null) 
    { 
     Reset(); 
     FilterName = name; 
     ControlType = type; 
     Load = load; 
     FromToType = ftType; 
    } 

    public void Reset() 
    { 
     From = null; 
     To = null; 
     SelectedFilter = null; 
     Items = new ObservableCollection<object>(); 
    } 
} 

अब मैं गतिशील रूप से liste.xaml फ़ाइल में अन्य xaml-files लोड करता हूं। उदाहरण के लिए Fahrzeug_Allgemein.xaml

वहाँ मैं comboboxes है, जो इस प्रकार है:

  <StackPanel> 
       <Label Content="Hersteller:" Target="{Binding ElementName=cmb_hersteller, Mode=OneWay}"/> 
       <ComboBox x:Name="cmb_hersteller" Fahrzeuge:FilterExtension.FilterName="hersteller" Width="120" ItemsSource="{Binding ctrlFilter[hersteller].Items}" SelectedIndex="0" SelectionChanged="cmb_hersteller_SelectionChanged"/> 
      </StackPanel> 

आप देखते हैं कि मैं फ़िल्टर वर्ग से आइटम संपत्ति प्राप्त करने के लिए कोशिश की, लेकिन यह नहीं है काम। विजुअल स्टूडियो 2015 उत्पादन में यह कहते हैं:

System.Windows.Data Error: 40 : BindingExpression path error: 'Items' property not found on 'object' ''Filter' (HashCode=44888241)'. BindingExpression:Path=ctrlFilter[hersteller].Items; DataItem='liste' (Name=''); target element is 'ComboBox' (Name='cmb_hersteller'); target property is 'ItemsSource' (type 'IEnumerable') 

मैं पहले से ही कुछ के बारे में INotifyPropertyChanged पढ़ा लेकिन मैं कैसे मेरे मामले में यह उपयोग करने के लिए पता नहीं है।

यह अच्छा होगा, अगर आप मेरी मदद कर सकते हैं। धन्यवाद।

+1

आप एक शब्दकोश से जुड़ने की कोशिश कर रहे हैं ... शायद यह आपकी मदद करेगा: http://stackoverflow.com/questions/3334128/binding-a-dictionarys-key-and-value-in-a-listbox- साथ-wpf – derape

उत्तर

1

मैंने नहीं सोचा है कि उत्तर इतना आसान है। मुझे बस आइटम संपत्ति में { get; set; } जोड़ना पड़ा।

तो कोड अब इस तरह दिखता है:

public class Filter 
{ 
    public string FilterName; 
    public Type ControlType; 
    public Type FromToType; 
    public bool Load; 
    public ObservableCollection<ComboBoxItem> Items { get; set; } 
    ... 

मुझे लगता है कि इसका मतलब है, कि आइटम संपत्ति इस के अलावा के साथ ही आबद्ध कर सकता है।

+1

सटीक होने के लिए, प्राप्त करने/सेट जोड़ने से पहले, यह केवल एक फ़ील्ड था, न कि संपत्ति। – WasGoodDone

1

मुझे इस पर टिप्पणी करने के लिए बहुत कम विनियमन है लेकिन आपको read up on properties in c# और data binding होना चाहिए यदि आपने अभी तक ऐसा नहीं किया है। मैंने इन लिंक को और अतिरिक्त लिंक शामिल किए हैं और इससे मेरी समझ में काफी वृद्धि हुई है।

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