Tuesday, February 21, 2012

Send Ping Command in C#

I had a scenario which need to test  whether server available or not for my application.So i wrote following code to accomplish that.

.net framework has set of classes which support to Ping to the specified host.

public bool IsAvailableHost(string hostName)
var ping = new Ping();
PingReply reply = ping.Send(hostName);
return reply != null && reply.Status == IPStatus.Success;
catch (Exception ex)
throw ex;

Set Alternative Background Color for Wpf ComboBox


Using style we can set the alternative background color of a combo box without any code behind.first we have to define the style that targets the combo box item.i have defined this style under the resources section of window

 <Style x:Key="alternativeStyle" TargetType="{x:Type ComboBoxItem}">
    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
              <Setter Property="Background" Value="Aqua"></Setter>
         <Trigger Property="ItemsControl.AlternationIndex" Value="1">
   <Setter Property="Background" Value="Red"></Setter>

*Note-:You have to defined AlternativeIndex as attached property(ItemsControl Class) Other wise Xaml parser doesn't recognize the AlternationIndex property

Then bind relevant style to the our combo box using Item Container Style

 <ComboBox ItemTemplate="{StaticResource ResourceKey=ListViewModel}" Margin="148,116,180,159" Name="cmbTest" IsDropDownOpen="True" AlternationCount="2" ItemContainerStyle="{StaticResource ResourceKey= alternativeStyle}" />

Finally it shows as…