Me, Silverlight, ComboCheck-mutant and Frank Sinatra


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:

  • 1 hour search on the net for something usefull; usefull means, of course, a copy/paste routine. I didn’t find. It looks like I am the single human facing that problem.
  • 2 hours drinking coffe, smoking and thinking very deep on why?, why should I have to do this… Yep. I finally got the expected explanation: a fucking butterfly-effect. 1 million years ago a nice carnivor simply missed the gran-gran-gran…-gran-gran mother of one of my relatives, a blondie so called the mitocondrial Eve. Lucky me.
  • 3 hours implementing stuf for the Combo-Check-Box mutant above. Sorry for mentioning this one…
  • some time smoking again, just for fun and hoping that my ex will know somehow that I still smoke. Don’t know what this should be counted for, but I had fun on this.:)

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 class FilterOption: INotifyPropertyChanged
{
bool _use = false;
public bool use
{
get { return _use; }
set{
if (value != _use)
{
_use = value;
onPropertyChanged(this, “use”); }
}
}

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 cmbFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//Important: nothing to do here :-)
}

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

….supposing that MyTask includes in definition some fields like account, project etc.
private List DoFilterText(List _data)
{
string filter = txtFilter.Text.ToLower().Trim();

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
free counters
(c) 2009 marius09.wordpress.com

One Response to Me, Silverlight, ComboCheck-mutant and Frank Sinatra

  1. Writing Jobs says:

    Great post thanks. I really enjoyed it very much. You have a great blog here. Thanks again for sharing.

    Love commenting, blogging, or writing? We would love for you to join us!

    - Writers Wanted -

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 49 other followers