I am trying to bind an enum to a combobox in a WPF-Usercontrol. I already got the different enum-values in, but the binding to my viewmodel doesn't work.
My Usercontroll:
UserControl
x:Class="XpTimeWriter.UserControls.FooterBar"
x:Name="me"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:enums="clr-namespace:XpTimeWriter.Enums"
xmlns:local="clr-namespace:XpTimeWriter.UserControls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="80"
d:DesignWidth="800">
<UserControl.Resources>
<ObjectDataProvider
x:Key="EnumDescription"
MethodName="GetValues"
ObjectType="{x:Type enums:Filter}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="enums:Filter" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
...
<ComboBox
Width="100"
DockPanel.Dock="Right"
ItemsSource="{Binding Source={StaticResource EnumDescription}}"
SelectedItem="{Binding SelectedFilter, ElementName=me}"
/>
...
The dependencyproperty behind:
public Filter SelectedFilter
{
get { return (Filter)GetValue(SelectedFilterProperty); }
set { SetValue(SelectedFilterProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedFilter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedFilterProperty =
DependencyProperty.Register("SelectedFilter", typeof(Filter), typeof(FooterBar), new PropertyMetadata(Filter.All));
My Binding-Property:
private Filter _filter;
public Filter SelectedFilter
{
get => _filter;
set
{
SetProperty<Filter>(ref _filter, value);
this.WorkTimes = LoadList(_filter);
}
}
The SetProperty-Method from my ViewModelBase:
protected bool SetProperty<T>(ref T backingStore, T value, Action onChanged = null, [CallerMemberName] string propertyName = "")
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
The enum:
public enum Filter
{
All,
LastWeek,
CurrentWeek,
FromTo
}
It seems as if the getter is working but on changes of the itemsource the setter is never executed.