
Row #2, Col #4:What about a beer tonight?
Row #8, Col #3:Nope. I have a date 1am
Row #3, Col #1:I'm in, I'm in!
Row #3, Col #2:Fuck you Jon! You'll stay home!
|
|
(c) marius09.wordpress.com |
I had the opportunity these days to use a lot fuck and shit words, and I am pretty gratefull for the universe for that. Tones of reasons. Particulary, for a mysterious one, I had to implement in Silverlight a custom filter for a List-based element.
So, let suppose that you are the lucky one who has to build a ComboBox, something like:

…and after selection is made, display something like:

…or, if a single item is checked:

…display the item itself:

I spent 6-7 hours on this issue, as it follows:
Ok.
Some stuff explaining why this should not be possible are here:
http://www.jigar.net/articles/viewhtmlcontent328.aspx
http://msdn.microsoft.com/en-us/library/ms668604(v=vs.95).aspx
http://stackoverflow.com/questions/7999317/silverlight-combobox-with-checkboxes-items
I am not very sure that you are interested on how I did it, but just in case, here are the basic stuff.
If you are a female and you are still reading these, something is definitively wrong with that butterfly-effect
1. Define a class implementing INotifyPropertyChanged
public string _field;
public string field
{ get{ return _field;}
set{
if(value!=_field){
_field=value;
onPropertyChanged(this, “field”);
}
}
}
bool _show = true;
public bool show
{
get { return _show; }
set
{
if (_show != value)
{
_show = value;
onPropertyChanged(this, “show”);
}
}
}
public string abr { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender,
new PropertyChangedEventArgs(propertyName));
if(propertyName.Equals(“show”))
PropertyChanged(sender,
new PropertyChangedEventArgs(“visibility”));
}
}
public Visibility visibility
{
get
{
return show ? Visibility.Visible : Visibility.Collapsed;
}
}
}
2. Define the ItemsSource
List FiltersOnTasks = new List();
private void SetFilters()
{
FiltersOnTasks = new List()
{
new FilterOption(){ use = true, field=”All”, abr=”All”, show=true },
new FilterOption(){ use = true, field=”Task name”, abr=”Tsk”, show=true },
new FilterOption(){ use = true, field=”Account”, abr=”Acc”, show=true },
new FilterOption(){ use = true, field=”Project”, abr=”Prj”, show=true },
new FilterOption(){ use = true, field=”Activity”, abr=”Act”, show=true },
new FilterOption(){ use = true, field=”Description”, abr=”Dsc”, show=true }
};
if (cmbFilter != null)
{
cmbFilter.ItemsSource = new ObservableCollection(FiltersOnTasks);
cmbFilter.SelectedIndex = 0;
}
}
3. XAML stuff
4. Combo-Check events
private void chkFilter_Checked(object sender, RoutedEventArgs e)
{
SetFilterSelected(sender);
}
private void chkFilter_Unchecked(object sender, RoutedEventArgs e)
{
SetFilterSelected(sender);
}
private void SetFilterSelected(object sender)
{
if (sender is CheckBox)
{
string originalField = (sender as CheckBox).Tag.ToString();
bool origianlIsChecked = (sender as CheckBox).IsChecked != null ? (bool)(sender as CheckBox).IsChecked : false;
ObservableCollection list = (ObservableCollection)cmbFilter.ItemsSource;
string multipleFields = “”;
string lastCheckedField = “”;
int noChecked = 0;
for (int i = 1; i 0 ? “, ” + list[i].abr : list[i].abr);
noChecked++;
lastCheckedField = list[i].field;
}
}
else
{
if (list[i].use)
{
multipleFields += (multipleFields.Length > 0 ? “, ” + list[i].abr : list[i].abr);
noChecked++;
lastCheckedField = list[i].field;
}
}
}
if (noChecked == list.Count – 1)
multipleFields = “All”;
if (noChecked == 1)
multipleFields = lastCheckedField;
if (noChecked == 0)
{
multipleFields = “All”;
for (int i = 1; i < list.Count; i++)
list[i].use = true;
(sender as CheckBox).IsChecked = true;
}
list[0].field = multipleFields;
if (cmbFilter.SelectedIndex != 0)
cmbFilter.SelectedIndex = 0;
}
}
private void cmbFilter_DropDownOpened(object sender, EventArgs e)
{
((ObservableCollection)cmbFilter.ItemsSource)[0].show = false;
cmbFilter.UpdateLayout();
}
private void cmbFilter_DropDownClosed(object sender, EventArgs e)
{
((ObservableCollection)cmbFilter.ItemsSource)[0].show = true;
cmbFilter.SelectedIndex = 0;
cmbFilter.UpdateLayout();
}
5. Filtering stuff
bool useAccount = UseFilterOn(“acc”);
bool useProject = UseFilterOn(“prj”);
bool useTaskName = UseFilterOn(“tsk”);
bool useDescription = UseFilterOn(“dsc”);
bool useActivity= UseFilterOn(“act”);
if (useAccount || useProject || useTaskName || useDescription || useActivity)
{
List x = _data.Where(t =>
(useAccount && t.account.name.ToLower().IndexOf(filter) > -1) ||
(useProject && t.project.name.ToLower().IndexOf(filter) > -1) ||
(useActivity && t.activity.name.ToLower().IndexOf(filter) > -1) ||
(useDescription && t.description.ToLower().IndexOf(filter) > -1) ||
(useTaskName && t.taskname.ToLower().IndexOf(filter) > -1)).ToList();
return x;
}
return _data;
}
private bool UseFilterOn(string _abr)
{
FilterOption filter = FiltersOnTasks.Where(f => f.abr.ToLower().Equals(_abr.ToLower()) && f.use).FirstOrDefault();
return filter!=null && filter.use;
}
That’s it.
……..and what about Frank Sinatra? – will ask someone ( I suppose that a carnivore is still desperately looking for him…).
Well.
Just because he did it his way, no copy/paste there….
……..so what? – could answer that one, who surely was recently escaped from zoo.
Fuck you, Eve!
|
|
(c) marius09.wordpress.com |