Saturday 19 November 2011

How to add image button Or How to add Image to Button in Silverlight?

How to add image button Or How to add Image to Button in Silverlight?

Ans=>
There is no need to add image tag or image button in the xaml file only add normal button to the xaml file like and add the image to the content property of the button it is same like Text property in .Net

   <Button.Content>
     <Image Height="20" HorizontalAlignment="Left" Name="imageTest" Stretch="Fill" Width="47" VerticalAlignment="Top" Source="D:\Projects\SilverlightApplication\Images\ABC.png" />
   </Button.Content>
</Button>

You can also add at runtime:

Add normal button to the page like

<Button x:Name="btnSave" x:Uid="1" Width="28" Height="28"></Button>

Then go to the code behind then depend on your condition you can add the following code into the various events like page load on click here I am adding image to the button in page load

  private void Page_Loaded(object sender, RoutedEventArgs e)
        {

   Uri urileft = new Uri("/SilverlightApplication/Images/ABC.png", UriKind.Relative);
                BitmapImage imgSource = new BitmapImage(urileft);
                Image image = new Image();
                image.Source = imgSource;

                btnSave.Content = image;
}

in the above code first four line is used to declare image object and to set image path then last two line is used where to add the image or we can say on which button u want to add image.
Like above code btnSave is normal button and content is property if u add
                btnSave.Content = image; //then image will set into the background of button.



<Button Height="24" Width="49" HorizontalAlignment="Left" Name=" btnSave " VerticalAlignment="Top">

Friday 18 November 2011

Using the App.Xaml for passing data between pages Or Storing And Retriving Data Globally

Suppose you have created user control in that you are getting values from the database and binding it to the datagrid and same data you want to access into the main page where this user control is added or some other page.
So Go to the App.xaml.cs in that create public property of the List or ObservableCollection like:
  public ObservableCollection<TblXyzDetail> OCTempTblXyzDetail { get; set; }
  public ObservableCollection<TblPQRDetail> OCTempLocationMasterDetail { get; set; }

in the above line TblXyzDetail & TblPQRDetail is my Class(i.e. view which is added into the edmx.)

after that initialize this object in startup event like

  private void Application_Startup(object sender, StartupEventArgs e)
        {
            //following lines are important to initialize the object.
            OCTempTblXyzDetail = new ObservableCollection<Web.Models.TblXyzDetail>();
            OCTempLocationMasterDetail = new    bservableCollection<Web.Models.TblPQRDetail>();
       
            System.Net.WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
            // This will enable you to bind controls in XAML files to WebContext.Current
            // properties
            this.Resources.Add("WebContext", WebContext.Current);

            // This will automatically authenticate a user when using windows authentication
            // or when the user chose "Keep me signed in" on a previous login attempt
            WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);
            // Show some UI to the user while LoadUser is in progress
            this.InitializeRootVisual();


}


After this go to the Usercontrol.cs page in that create object of the app.xmal like

private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
     //here I am creating object of the app.xmal page
     App app = (App)Application.Current;

     //here i am creating the new public property of the observable collection and also initializing
             public ObservableCollection<TblXyzDetail> OCTblXyzDetail { get; set; }
OCTblXyzDetail = new ObservableCollection<TblXyzDetail>();


     //here I am initializing the object of the app.xmal page
            app.OCTempTblXyzDetail = new ObservableCollection<TblXyzDetail>();           
        }


So you can access public variables, objects and functions etc. after that assign the data to the OCTempTblXyzDetail  object of the app.xaml.cs like

Here OCTblXyzDetail is my main object of the user control in that I am storing the databse values at the time of page load. Like

private void GetAllAttributeNameByDataDimensionIDQuery_Complete(LoadOperation<TblXyzDetail> operation)
        {
            if (!operation.HasError)
            {
                OCTblXyzDetail = operation.Entities.ToObservableCollection();
                if (OCTblXyzDetail.Count > 0)
                {
Datagrid.ItemsSource = OCTblXyzDetail;
//follwoing line is used to assign the data to the object of the app.xaml page
app.OCTempTblXyzDetail = OCTblXyzDetail;

                }
            }
        }



Now you can get the data from anywhere in application. Here I am retrieving  this data into the my main page here also you have to create the object of the app.xaml page like 
   private void Page_Loaded(object sender, RoutedEventArgs e)
    {
//here I am creating object of the app.xmal page
       App app = (App)Application.Current;
ObservableCollection  OCTblXyzDetail = new ObservableCollection<TblXyzDetail>();

//Following line is used to get data from the object of the app.xaml
OCTblXyzDetail =app. OCTempTblXyzDetail.ToObservableCollection();
    }
Now you can use OCTblXyzDetail to bind to the list or to data grid.