Wednesday, May 23, 2012

Sort observable collection object

scriptInfo.OrderBy(i => i.time_start);
where time_start is a property of a strongly typed class. Cheers!

Tuesday, May 22, 2012

WPF Scroll Viewer slow performance (Bind control height from another object using code)

In this post, I created a datagrid dynamically or from code. When a record is loaded, the scrolling is slow using Scrollviewer control.

So to translate into code behind here's what i come up..
Binding bindHeight = new Binding();
bindHeight.ElementName = String.Format("sizingElement{0}", index + 1);
bindHeight.FallbackValue = 1;
bindHeight.Path = new PropertyPath("ActualHeight");
//mapGrid is the datagrid object
mapGrid.SetBinding(DataGrid.HeightProperty, bindHeight);

XAML Markup:
<StackPanel x:Name="spThur" Height="Auto" Background="Green" HorizontalAlignment="Left" 
Orientation="Horizontal">
         <Rectangle Name="sizingElement4"  Fill="Transparent"  Margin="1" Height="333"/>
 </StackPanel>


the mapGrid datagrid is added to the stackpanel...and the Rectangle object is the sizing element..



(I found the solution from this site)
http://stackoverflow.com/questions/7254362/scrollviewer-slow-perfomance-with-datagrid

Monday, May 21, 2012

WPF Set Color Value based from HEXA

Here's a snippet that passes a hexa color and set a wpf control object's background/foreground property.

BrushConverter bc = new BrushConverter(); 



                Brush brush; 

                brush = (Brush)bc.ConvertFrom("#003F87"); 

                notes.SetValue(TextBlock.BackgroundProperty, brush);



Original Source: http://jammer.biz/blog2/?p=169
Cheers,

Greg

Wednesday, May 9, 2012

Access Datagrid Row in WPF through index


This is just a repost from this site:

http://www.codefound.net/2011/03/accessing-datagridrow-from-wpf-datagrid.html


DataGridRow dgr = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(nRowIndex);

if (dgr == null) // row might be invisible (top / bottom). So, scroll into the view and get it

{

    dataGrid1.UpdateLayout();

    dataGrid1.ScrollIntoView(dataGrid1.Items[nRowIndex]);

    dgr = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(nRowIndex);

}



Thanks for the original contributor, this helped me a lot..

Cheers!

Tuesday, May 8, 2012

Loop Through Datagrid in WPF (ItemSource is type Observable Collection)


Assuming the databinding source of WPF Datagrid is of type ObservableCollection


var row = GetDataGridRows(dgScripts);

foreach (DataGridRow r in row)

{

     CScriptsInfo info = (CScriptsInfo)r.Item;

     if(info.prog_name.ToString().Contains(txtScriptName.Text.Trim().ToUpper()))

     {

         r.IsSelected = true;

     }

}




Sunday, May 6, 2012

Count Record in every hour of a given day (MySQL)


Here is a query to count records gathered in every hour of a given day. The date_visited is a 24 hour time format which is of timestamp data type. The idea came from a fellow developer..


select

    hour(time(date_visited)) as hour_of_day_24_hour_format,

    count(hour(time(date_visited))) as count_per_hour from tblpropsale_v2

   where date(date_visited) = '2012-05-07' and web_id = 642 and is_visited = 1

     group by hour(time(date_visited)) order by hour(time(date_visited)) asc;




Thursday, April 19, 2012

Regex in html list tag not working (multiline string)

I have this string: < li class="MsoNormal" style="color:white;mso-themecolor:background1; mso-margin-top-alt:mso-list:l1 level1 lfo1;tab-stops:list 36.0pt"> and the regex has a lazy quantifier to replace the string above to < li>. Using a simple regex pattern, it won't change the string above to ordinary < li>. The solution would be to add a regex to replace new line characters. Since the string has newline characters.
Regex.Replace(source, @"\n", string.Empty, RegexOptions.IgnoreCase);