I have the following Enum:
[Flags]
public enum SubscriberLevel {
BRONZE = 0,
SILVER1 = 1,
SILVER2 = 2,
SILVER = SubscriberLevel.SILVER1 | SubscriberLevel.SILVER2,
GOLD1 = 4,
GOLD2 = 8,
GOLD3 = 16,
GOLD = SubscriberLevel.GOLD1 | SubscriberLevel.GOLD2 | SubscriberLevel.GOLD3,
DIAMOND1 = 32,
DIAMOND2 = 64,
DIAMOND3 = 128,
DIAMOND = SubscriberLevel.DIAMOND1 | SubscriberLevel.DIAMOND2 | SubscriberLevel.DIAMOND3,
ALL = SubscriberLevel.BRONZE | SubscriberLevel.SILVER | SubscriberLevel.GOLD | SubscriberLevel.DIAMOND
}
I am working on a control that should allow a user to dictate one, many, or all flags in this enum through a series of checkboxes, one for each Enum in the list.
I have an Enum member in the control, and a property to expose the Enum. The control itself inherits the INotifyPropertyChanged method, and the Setter of the property correctly calls the OnPropertyChanged event handler when the value gets set.
I even see some semblance of functionality within the control, but what I don't see is it behaving properly:
Say user so and so unchecks the ALL option; I want every single check box to reflect this by being cleared as well, but I am not seeing that happen.
As of right now, I think my problem is that I am binding the Checked state of these checkboxes to the Enum itself, and what I need to be doing is binding the Enum to these checked boxes, and have each check box represent a specific flag in the Enum set.
I've noticed in debugging the control that, when I uncheck the check box, the value is not getting set. Why is that?
I know I'll need to use a converter to accomplish this, but for right now; how do I go about accomplishing this on the XAML side; defining the Datacontext of the control as the control itself, and then tieing the Enum flags to their respective check boxes?
This is an example of one of the check boxes :
<CheckBox
x:Name="chkAll" Grid.Row="1" VerticalContentAlignment="Center" VerticalAlignment="Center"
IsChecked="{Binding Path=SubLevel, Converter={StaticResource ETBC},
ConverterParameter={x:Static Enums:SubscriberLevel.ALL}, Mode=TwoWay}"/>
This is the Converter:
public class EnumToBoolConverter : IValueConverter {
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
return ( ( Enum )value ).HasFlag( ( Enum )parameter );
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture ) {
return value.Equals( true ) ? parameter : Binding.DoNothing;
}
}
This is the code for the control (as it stands):
public partial class UCSubscriptionLevels : UserControl, INotifyPropertyChanged {
private SubscriberLevel _SubLevel = SubscriberLevel.ALL;
public event PropertyChangedEventHandler PropertyChanged;
public UCSubscriptionLevels( ) { InitializeComponent( ); }
/// <summary>
/// Get or Set Subscriber Levels to Build.
/// </summary>
public SubscriberLevel SubLevel {
get { return this._SubLevel; }
set {
this._SubLevel = value;
if ( this.PropertyChanged != null )
this.PropertyChanged( this, new PropertyChangedEventArgs( "SubLevel" ) );
}
}
}
Aucun commentaire:
Enregistrer un commentaire