mardi 16 août 2016

ComboBox CheckBox WPF Converter C#

I have just started learning how to use WPF and I am fairly new to this technology.I need to add a Select All Checkbox within my ComboBox but the issue with my current piece of code is that when I do select any other checkbox than the select all it is checked along with the checkbox I want to check/filter from a conversation list(like Excel does). I have tried adding it to an Array within the and that didn't work, is it possible to create a Style to handle this within XAML? Or is there a better approach to fixing this?

PersonHistory.xaml

<ComboBox
IsEditable="True"
IsReadOnly="True">
<CheckBox Content="All Calls" Style="{StaticResource FilterCheckBox}"
       IsChecked="{Binding ConversationViewModel.ConversationValues, Convert={StaticResource PersonFilterTypeToBoolConverter}, ConverterParameter={x:Static shared:MethodType.All}}" />
<CheckBox Content="Mobile Call" Style="{StaticResource FilterCheckBox}"
       IsChecked="{Binding ConversationViewModel.ConversationValues, Convert={StaticResource PersonFilterTypeToBoolConverter}, ConverterParameter={x:Static shared:MethodType.Mobile}}" />
<CheckBox Content="Skype Call" Style="{StaticResource FilterCheckBox}"
       IsChecked="{Binding ConversationViewModel.ConversationValues, Convert={StaticResource PersonFilterTypeToBoolConverter}, ConverterParameter={x:Static shared:MethodType.Skype}}" />
<CheckBox Content="Landline Call" Style="{StaticResource FilterCheckBox}"
       IsChecked="{Binding ConversationViewModel.ConversationValues, Convert={StaticResource PersonFilterTypeToBoolConverter}, ConverterParameter={x:Static shared:MethodType.Landline}}" />
</ComboBox>

PersonFilterTypeToBoolConverter.cs

public class PersonFilterTypeToBoolConverter : IValueConverter
{
   private MethodType currentMethod = MethodType.Unknown;
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                {
                        this.currentMethod = (MethodType)value;
                        return (this.currentMethod & (MethodType)parameter) != 0;
                }

                public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                {
                       this.currentMethod ^= (MethodType)parameter;
                       return this.currentMethod;
                }
}

A .Gif of what I want to achieve but with a Select all CheckBox

I haven't seen any answers on here that have both the Convert and ConvertParameter(Which is a Flag Enum) and a lot of the ones I have seen are 4 years old so I was wondering if there was an update way of handling ComboBox CheckBoxes.

I have tried to change the Convert method to

if (this.currentMethod != MethodType.All){
    return (this.currentMethod & (MethodType)parameter);
} else {
    return (this.currentMethod & (MethodType)parameter) != 0;
}

This does fix the Click issue as it was the wrong way around but now the Checkboxes don't really work properly as I can only get single checks in the boxes not All.




Aucun commentaire:

Enregistrer un commentaire