将Xaml代码转换为c#

时间:2014-07-23 05:23:45

标签: 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="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>

3 个答案:

答案 0 :(得分:1)

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

<Control x:Name="MyNamedControl" />

然后你可以在C#中执行此操作:

MyNamedControl.PropertyToSet = Value;

答案 1 :(得分:0)

首先,将每个控件分配为Name="",然后在C#代码中,您可以使用其名称访问这些值。

ControlName.Property = Property.Value;

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

答案 2 :(得分:-1)

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

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