-2

I need too add some control unit such as Grid, Checkbox ,Textblock and ... dynamically in my C# Code.

Assume XAML node like:

<CheckBox Content="CheckBox" Height="24" Click="CheckBoxes_Click"/>

Its C# equivalent is

AddNewCheckBox()
{
     CheckBox NewCheckBox = new CheckBox ();
     NewCheckBox.Content = "CheckBox1";
     NewCheckBox.Height = 24;
     NewCheckBox.Click += CheckBoxes_Click;
}

But there are many XAML assignment which it is hard to understand their C# equivalent. As an example what should I write in my c# to create a CheckBox like this?

<CheckBox Content="CheckBox" Margin="68,41,0,0" Background="Black"
          Height="Auto" Click="CheckBoxes_Click"/>

Is there any way to understand how XAML parser maps phrases to C# code?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
D.Ghiaseddin
  • 115
  • 9

4 Answers4

2

Is there any way to understand how XAML parser maps phrases to C# code?

Looking at this example:

<CheckBox Content="CheckBox"
          Margin="68,41,0,0"
          Background="Black"
          Height="Auto"
          Click="CheckBoxes_Click"/>

If we want to understand how the XAML parser knows how to set more complicated properties (ones that cannot simply use the TryParse() methods of the types) we need to look at the types of the properties.

If you look at the Margin property for example it is of type Thickness and if you look at that type you will find this attribute:

[TypeConverter(typeof(ThicknessConverter))]

If you look at that type (in PresentationFramework.dll) with for example dotPeek you will find ConvertFrom(...) and ConvertTo(...) methods that take care of the conversion. The internal method FromString(...) contains the relevant parts for this example.

Carl Serrander
  • 346
  • 1
  • 7
0

To create checkbox like that you should write it like this:

AddNewCheckBox()
{
     CheckBox NewCheckBox = new CheckBox ();

     NewCheckBox.Content = "CheckBox1";
     NewCheckBox.Height = 24;
     NewCheckBox.Click += NewCheckBox_Click;
     NewCheckBox.Margin = new Thickness(64, 41, 0, 0);
     NewCheckBox.Background = new SolidColorBrush(Color.Black);
     //or like this: NewCheckBox.Background = Brushes.Black;
}
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
0

what should I write in my c# to create a check box like this?

<CheckBox Content="CheckBox" 
          Margin="68,41,0,0" 
          Background="Black" 
          Height="Auto" 
          Click="CheckBoxes_Click"/>

The above equates to

var checkBox = new CheckBox () {
    Content = "CheckBox",
    Margin = new Thickness(64, 41, 0, 0),
    Background = Brushes.Black,
    Height = Double.NaN              
};
checkBox.Click += CheckBoxes_Click 
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

As an example what should I write in my c# to create a CheckBox like this?

The same more or less. Each attribute in XAML maps to a property in C#. So the equivalent would be:

CheckBox checkBox = new CheckBox();
checkBox.Content = "CheckBox";
checkBox.Margin = new Thickness(68,41,0,0);
checkBox.Background = Brushes.Black;
checkBox.Click += CheckBoxes_Click;

The type of the Background property is Brush. And the type of the Margin property is Thickness. You can confirm this by looking at the documentation on MSDN.

The XAML processor is able to translate the string "Black" to a Brush and the value "68,41,0,0" to a Thickness for you. The C# compiler is not. Apart from this, you are setting the exact same properties of the exact same class.

mm8
  • 163,881
  • 10
  • 57
  • 88