Bind StackPanel Visiblility to Radiobutton IsChecked

First steps with Silverlight …

The context :

I have some radiobuttons, and a stackpanel I want to be visible if one or the radiobutton is checked

The solution I found :

1) Write the code

The radio buttons


// ...

<RadioButton Name="SomeRadio"  IsChecked="True" Content="some radio/>
 <RadioButton Name="WantedRadio" Content="the wanted radio"/>

// ...

The StackPanel


// ...

<StackPanel Name="ThePanel">

// some content

</StackPanel>

2) Convert

Since the Visibility property type  is an enum and the Ischeckd a boolean, we need a converter

Create it : you must implement the IValueConverter Inerface which is in the “System.Windows.Data” namespace


using System;
using System.Windows;
using System.Windows.Data;

namespace TheNamespace
{
 public class VisibilityConverter : IValueConverter
 {
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
// just in case
if (value == null)
{
return Visibility.Collapsed;
}

bool ischecked = System.Convert.ToBoolean(value);
return ischecked ? Visibility.Visible : Visibility.Collapsed;
 }

// had to be implemented
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
 }
}

Reference the namespace of the converter in the xaml file


xmlns:local="clr-namespace:TheNamespace"

Add the class as ressource for the user control

<UserControl.Resources>

 <local:VisibilityConverter x:Key="VisibilityConverter" />
</UserControl.Resources>

And now you can use it to bind the properties


Visibility="{Binding IsChecked,  ElementName=WantedRadio, Converter={StaticResource  VisibilityConverter}}">

Incoming search terms:

  • binding visibility to radiobutton
  • asp bind textbox visiblility to radiobutton
  • vnext radio button null
  • visibility converter bound to radio button ischecked property windows phine
  • stackpanel visibility binding
  • silverlight binding null radio buttons
  • radiobutton change visibility stackpanel
  • how to bind radio button in with windows phone
  • Bind visibility to radiobutton checked
  • bind coontrol visibility to radion button

Image to byte array and vice-versa

This is an old code snippet I posted on the www.developpez.com faq a few years ago.

I needed it today, and I decided to post it here

Image to byte array :


public static Byte[] ImageToByteArray(Image img)
{
try
{
MemoryStream mstImage = new MemoryStream();
img.Save(mstImage, System.Drawing.Imaging.ImageFormat.Jpeg);
Byte[] bytImage = mstImage.GetBuffer();
return bytImage;
}
catch (Exception ex)
{
// do something smart
return null;
}
}

Image to Byte array


public static Image ByteArrayToImage(Byte[] BArray)
{
try
{
MemoryStream mstImage = new MemoryStream(BArray);
Image img = Image.FromStream(mstImage);
return img;
}
catch (Exception ex)
{
// do something smart
return null;
}
}

http://dotnet.developpez.com/faq/csharp/?page=gdiplus#gdip_convert_image_bytes

Incoming search terms:

  • memorystream from controls image
  • convert image to byte[] in wp7
  • mstimage
  • byte array to writeablebitmap
  • byte[] to writablebitmap wp8
  • convert byte array to writeablebitmap net
  • csharp image to byte array
  • writeablebitmap to byte array wp8
  • how to bind image from byte in C# net
  • how to use the system windows media imaging to convert an image to byte[]

The IX509CertificateRequestPkcs10 InitializeFromTemplateName adventure

This week has been researches, tests and headaches to be able to make request on a Certificate Authority server from a web application.

My client has a Win server 2008 CA server for my developments

On this server I have a certificate template named “User Template”

Certificate_server_templates

My assignment was to request a certificate via an ASP.NET application.

After researches on how to do that, I found this blog post that helped me a lot : http://blogs.msdn.com/alejacma/archive/2008/09/05/how-to-create-a-certificate-request-with-certenroll-and-net-c.aspx

In my development environment, I create a request for a certificate by using  this code

 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = "User Template";
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
   log.DebugFormat("Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} ",
    ex.Message,
    ex.InnerException,
    ex.StackTrace,
    ex.Source,
    ex.TargetSite);
 }

And it worked just fine !

Then we moved the published solution to the staging environment, and the problems arrived . . .

It didn’t worked !

The error message said:

“CertEnroll::CX509CertificateRequestPkcs10::InitializeFromTemplateName: The requested certificate template is not supported by this CA. 0×80094800 (-2146875392)”

stack :

“at CERTENROLLLib.CX509CertificateRequestPkcs10Class.InitializeFromTemplateName(X509CertificateEnrollmentContext Context, String strTemplateName)”

cererror

We checked the security everywhere ( CA server, Certificate templates, IIS Account, …), give access to everyone on the CA, on the template, … without any success

Then, after days and hours of search and tests, I decided to re-read the method documentation : http://msdn.microsoft.com/en-us/library/aa377533%28v=VS.85%29.aspx

And I noticed the second parameter description:

strTemplateName [in]
Pointer to a BSTR variable that contains the Common Name (CN) of the template as it appears in Active Directory or the dotted decimal object identifier.

I had never tried using the dotted decimal object identifier, so i give it a shot.

I retrieved the object identifier on a certificate previously created with the “User Template”

On the CA server, I had a certificate request with the template “User Template”, so I right-click on it, go to All Tasks and click on the “View attributes/Extensions. . .” menu item.

image

image

A property windows opened, I go to the Extensions tab and click on the “Certificate Template Information” item.

image

In the information panel, I can see the famous dotted decimal object identifier between brackets.

I copy-paste this identifier in my code :

 CERTENROLLLib.CX509CertificateRequestPkcs10Class request = new CERTENROLLLib.CX509CertificateRequestPkcs10Class();
 string templateName = "1.3.6.1.4.1.311.21.8.3531346.8488945.6374567.164989.5001604.52.12582268.10747996";
 try
 {
    request.InitializeFromTemplateName(CERTENROLLLib.X509CertificateEnrollmentContext.ContextUser, templateName);
 }
 catch (Exception ex)
 {
    log.DebugFormat("Error InitializeFromTemplateName : message {0}, inner : {1}, stack : {2}, source : {3}, target : {4} ",
      ex.Message,
      ex.InnerException,
      ex.StackTrace,
      ex.Source,
      ex.TargetSite);
 }

I compiled, I tried in the development environment and it worked !!

(note: there is just a minimal change in my code since I put the “templateName” in a configuration file in order to be able to modify it)

So we moved the published solution to the staging environment,

retrieve the dotted decimal object identifier corresponding to the “User Template” on the staging CA server,

copy-pasted this identifier in the configuration file,

executed a “IISRESET” command and try to execute the application.

And it worked !

We can’t figured out why it worked in the development environment but not in the staging one while both are equals. So If anyone knows about it, please let me know.

So, don’t trust the template name and use the object identifier !

Incoming search terms:

  • CX509CertificateRequestPkcs10Class
  • ix509
  • InitializeFromTemplateName
  • ix509certificaterequestpkcs10 c#
  • Certenrolllib Template
  • CX509CertificateRequestCmcClass
  • TheIX509CertificateRequestPkcs10InitializeFromTemplateNameadventure
  • cx509certificaterequestpkcs10class msdn
  • CERTENROLLLib X509CertificateEnrollmentContext ContextUser
  • InitializeFromTemplateName certenroll