Wednesday, September 09, 2009

Billiards Game developed in Silverlight

I have developed Billiards game in Silverlight .This game supports 8 ball and Pool. Below are the screens of this game and to play this game please click here



How to get content and text of each element of a Window

 Some times it is needed to get the content/text of  the children of a control.Then we have to iterate through all elements and should know its type.After comparing its type with the predefined types, we can cast that UIElement object to the particular object type.Then we can get/set its text/content values.Here is the sample code.

        foreach (UIElement oObject in this.LayoutRoot.Children)
            {

                Type type= oObject.GetType();
                string name=oObject.GetValue(NameProperty).ToString();
                if (type == typeof(TextBlock))
                {
                    TextBlock textBlock = oObject as TextBlock;
                    if (textBlock != null)
                    {
                        MessageBox.Show(textBlock.Text);
                    }
                }

                if (type == typeof(TextBox))
                {

                    TextBox textBox = oObject as TextBox;
                    if(textBox != null)
                    {

                        MessageBox.Show(textBox.Text);
                    }
                }
                if(type == typeof(ComboBox))
                {
                    ComboBox comboBox = oObject as ComboBox;

                    if(comboBox != null)
                    {

                        MessageBox.Show(comboBox.SelectedItem.ToString());

                    }

                }

                if(type==typeof(Button))

                {

                    Button button = oObject as Button;

                    if(button != null)

                    {

                        MessageBox.Show(button.Content.ToString());

                    }
                }        
}

Tuesday, September 08, 2009

.Net Technology Quest: Silverlight ComBoBox Selected Item not Setting

.Net Technology Quest: Silverlight ComBoBox Selected Item not Setting

Silverlight ComBoBox Selected Item not Setting

Here is the code I developed to Bind data to a ComboBox and set the selected Item. This code is tested and working fine. Xaml page

<UserControl x:Class="WorldOfVenkat.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="900" Height="600">

<Canvas x:Name="LayoutRoot" Background="Black">

<ComboBox Name="comBoBox" Width="400" Height="30" ></ComboBox>

</Canvas>

</UserControl>

Code behind in C#

public partial class Page : UserControl

{ public Page()

{

InitializeComponent(); BindComBoBox(); }

private void BindComBoBox()

{

List<Country> coutries = new List<Country>();

Country India = new Country(1,"India");

Country USA = new Country(2, "USA");

Country Canada = new Country(3,"Canada");

coutries.Add(India);

coutries.Add(USA);

coutries.Add(Canada);

comBoBox.ItemsSource = coutries; comBoBox.DisplayMemberPath = "Name"; comBoBox.SelectedItem = Canada;

}

public class Country

{

public int ID { get; set; }

public string Name { get; set; }

public Country(int id,string name)

{

ID = id;

Name = name;

}

} }