ayman
ayman

Reputation: 1

Convert Xaml code to c#

I need to convert XAML TO C# in the following code.

It is working fine in the design time.

but i want to assign the values in the runtime. Here goes the XAML code

<ListBox Height="550" Name="listBox1" Width="398" FontFamily="Calibri" Opacity="20" FontStretch="Normal" SelectedIndex="1" FontWeight="Bold" FontStyle="Normal" FontSize="28" HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalContentAlignment="Center" SelectionChanged="listBox1_SelectionChanged" BorderBrush="#FF828790" Foreground="Red" OpacityMask="{x:Null}">

                            <Border CornerRadius="6" BorderBrush="Black" Background="White" BorderThickness="1" DockPanel.Dock="Top" AllowDrop="True">
                        <StackPanel Orientation="Horizontal" DataContext="{Binding}" Width="288">
                            <Image Source="/final;component/Images/shawshank.jpg" Width="75" Stretch="Fill" DataContext="{Binding}" FlowDirection="LeftToRight" Height="75"></Image>
                            <StackPanel Orientation="Vertical" DataContext="{Binding}" Width="288">
                                <TextBlock Text="The Shawshank" FlowDirection="LeftToRight" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="22" Foreground="Black"></TextBlock>
                                <Image Height="22" Source="/final;component/Images/fivestars.png" Width="100" IsManipulationEnabled="False" Stretch="Fill" StretchDirection="Both" FlowDirection="LeftToRight" DataContext="{Binding}" Margin="0" AllowDrop="False" ClipToBounds="False" Focusable="False" OverridesDefaultStyle="False" UseLayoutRounding="False" HorizontalAlignment="Left"></Image>
                                    <TextBlock Text=" By:Frank Darabont" FlowDirection="LeftToRight" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="Segoe UI" FontSize="18" Foreground="Black" FontWeight="Normal"></TextBlock>
                                </StackPanel>
                        </StackPanel>
                    </Border>
                    </ListBox>

Upvotes: 0

Views: 1594

Answers (3)

Vivek Saurav
Vivek Saurav

Reputation: 2275

You can add controls and the property for the elements in the code behind like this :-

//Creating Controls    
ListBox l1 = new ListBox();
                l1.Height = 550;
                l1.Width = 398;
//Adding Property to controls
                l1.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                l1.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
                l1.Name = "listBox1";
                Border bd = new Border();
                StackPanel sp = new StackPanel();
                Button btn = new Button();

    //Adding Controls to the Containers
                sp.Childern.Add(btn);
                bd.Child = sp;
                l1.Items.Add(sp);

Upvotes: 0

Marcel B
Marcel B

Reputation: 3674

There is no need to convert your whole XAML to C# to do this. If you want to set values from C# your control needs a name. To name a control do this in XAML:

<Control x:Name="MyNamedControl" />

Then you can do this in C#:

MyNamedControl.PropertyToSet = Value;

Upvotes: 1

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15870

First thing would be to assign each of this control a Name="" and after that in your C# code you can access the values, using their names.

ControlName.Property = Property.Value;

This is a simple method you can access currently present controls or you can create your own control on the runtime and then assign value to them and attach them to the software Display.

Upvotes: 0

Related Questions