Databinding WPF combobox doesn't work twoway
I have a combobox, which is filled with enums via data binding and this works quite well. Also the binding of the selected item to a property works fine.
I have set the binding to the property to TwoWay, but if the property MyDbType changes, the combobox changes not.
XAML:
<Wi开发者_如何学编程ndow.Resources>
<ObjectDataProvider x:Key="dbEnum" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="utils:DbType" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<ComboBox Margin="0" Name="comboDbType" VerticalAlignment="Center" Grid.Row="1" Height="25"
ItemsSource="{Binding Source={StaticResource dbEnum}}"
SelectedItem="{Binding Path=CurrDbSettings.MyDbType,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:MainWindow}},
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Grid.ColumnSpan="1" Grid.Column="1">
</ComboBox>
Any hints?
The property MyDbType
needs to somehow tell the binding system that it has changed. For that, there is the INotifyPropertyChanged
interface which you need to implement on the CurrDbSettings object's class and rise the PropertyChanged
event in the setter of the MyDbType
property. Something like this:
public class DbSettings : INotifyPropertyChanged
{
...
public MyTypeEnum MyDbType
{
get { return _myDbType; }
set
{
_myDbType = value;
RaisePropertyChanged("MyDbType");
}
}
...
}
精彩评论