Habe ein UserControl gebaut.
<UserControl ... >
<Grid x:Name="ElementRoot" DataContext="{Binding ElementName=ParentElement}">
...
<ComboBox x:Name="CountryComboBox"
Grid.Row="3"
Grid.Column="1"
Margin="4"
DisplayMemberPath="Name"
ItemsSource="{Binding CountryList,
Mode=TwoWay}"
SelectedItem="{Binding SelectedItem}" />
...
</Grid>
</UserControl>
Code behind mit den Dependency Properties
public partial class AddressControl : UserControl
{
public AddressControl()
{
InitializeComponent();
}
public static readonly DependencyProperty StreetProperty = DependencyProperty.Register(
"Street", typeof(string), typeof(AddressControl), new PropertyMetadata(default(string)));
public string Street
{
get { return (string)GetValue(StreetProperty); }
set { SetValue(StreetProperty, value); }
}
public static readonly DependencyProperty CityProperty = DependencyProperty.Register(
"City", typeof(string), typeof(AddressControl), new PropertyMetadata(default(string)));
public string City
{
get { return (string)GetValue(CityProperty); }
set { SetValue(CityProperty, value); }
}
public static readonly DependencyProperty ZipCodeProperty = DependencyProperty.Register(
"ZipCode", typeof(string), typeof(AddressControl), new PropertyMetadata(default(string)));
public string ZipCode
{
get { return (string)GetValue(ZipCodeProperty); }
set { SetValue(ZipCodeProperty, value); }
}
public static readonly DependencyProperty CountryListProperty = DependencyProperty.Register(
"CountryList", typeof(ObservableCollection<Country>), typeof(AddressControl),
new PropertyMetadata(default(ObservableCollection<Country>)));
public ObservableCollection<Country> CountryList
{
get { return (ObservableCollection<Country>)GetValue(CountryListProperty); }
set { SetValue(CountryListProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem", typeof(Country), typeof(AddressControl), new PropertyMetadata(default(Country)));
public Country SelectedItem
{
get { return (Country)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
}
Die ItemsSource der Combobox wird an eine Liste mit Countries gebunden. Das UC funktioniert soweit, dass ich die Liste bekomme, anzeigen kann, auswählen kann, und das Model auf das zugegriffen wird, die Werte auch wieder gesetzt bekommt.
Das UserControl wird folgend verwendet:
<StackPanel Orientation="Vertical">
<controls:AddressControl City="{Binding City,
Mode=TwoWay}"
CountryList="{Binding Countries,
Mode=TwoWay}"
SelectedItem="{Binding Country,
Mode=TwoWay}"
Street="{Binding Street,
Mode=TwoWay,
ValidatesOnDataErrors=True}"
ZipCode="{Binding ZipCode,
Mode=TwoWay,
ValidatesOnDataErrors=True}" />
Wenn ich nun ein Address item mit gefüllten Werten dem UC zum Binden gebe, werden auch alle Daten, außer der Combobox, korrekt angezeigt und können geändert und gespeichert werden.
Allerdings zeigt die Combobox den Wert nicht an.
Versuchsweise wurde auch das SelectedValue der Combobox an das SelectedItem Property gebunden. Brachte allerdings auch nicht den erwünschten Effekt.
Hat jemand eine Idee was ich hier falsch mache?
Grüße