将Xaml代码转换为c#

将Xaml代码转换为c#,c#,wpf,xaml,C#,Wpf,Xaml,我需要在下面的代码中将XAML转换为C# 它在设计阶段运行良好 但是我想在运行时分配值。下面是XAML代码 <ListBox Height="550" Name="listBox1" Width="398" FontFamily="Calibri" Opacity="20" FontStretch="Normal" SelectedIndex="1" FontWeight="Bold" FontStyle="Normal" FontSize="28" HorizontalAlignment

我需要在下面的代码中将XAML转换为C#

它在设计阶段运行良好

但是我想在运行时分配值。下面是XAML代码

<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>

第一件事是为每个控件分配一个
Name=”“
,然后在C代码中,您可以使用它们的名称访问这些值

ControlName.Property = Property.Value;

这是一种简单的方法,您可以访问当前的控件,也可以在运行时创建自己的控件,然后为其赋值并将其附加到软件显示。

无需将整个XAML转换为C。如果要从C#设置值,控件需要一个名称。要命名控件,请在XAML中执行此操作:

<Control x:Name="MyNamedControl" />

您可以在代码隐藏中为元素添加控件和属性,如下所示:-

//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);

这似乎并不难。你到底有什么问题?我想从代码中将项目添加到我的列表框中。但是我不知道如何为所有项目保持相同的设计我不知道列表框的板条箱模板是否可以解决问题你能在问题中提到你的自定义模板吗?它是borader,在边框内有两个堆栈窗格,其中一个有图像,另一个有其他有图像和文本
//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);