Sono nuovo alla programmazione WPF e non professionale nella codifica C#.WPF Collegamento a dizionario con classi
Quindi ho il seguente problema: Ho un sacco di combobox e caselle di testo diverse. Voglio associarli a una variabile in background, perché voglio riempire le caselle combinate con Dati da un database.
Così ho creato il seguente nel file
liste.xaml.cs:
private Dictionary<string, Filter> _ctrlFilter;
public Dictionary<string, Filter> ctrlFilter {
get { return _ctrlFilter; }
set { _ctrlFilter = value; }
}
Quando carico liste.xaml si inizializza il dizionario ctrlFilter con il seguente codice:
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)) },
};
Quindi la classe filtro (che si trova anche in liste.xaml.cs) è simile a questa:
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>();
}
}
Ora ho caricare dinamicamente altri XAML file nel file liste.xaml. Ad esempio, il Fahrzeug_Allgemein.xaml
Non ho le caselle combinate, che assomiglia a questo:
<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>
Si vede che ho cercato di ottenere la proprietà Items dalla classe del filtro, ma non è così lavoro. In Visual Studio 2015 in uscita che dice:
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')
ho già letto qualcosa su INotifyPropertyChanged
ma non so come usarlo nel mio caso.
Sarebbe bello, se potessi aiutarmi. Grazie.
Stai cercando di associare a un dizionario ... Forse questo ti aiuterà: http://stackoverflow.com/questions/3334128/binding-a-dictionarys-key-and-value-in-a-listbox- with-wpf – derape