【问题标题】:Convert XAML code to C# code将 XAML 代码转换为 C# 代码
【发布时间】:2015-02-16 20:33:04
【问题描述】:

如何将此代码部分从 XAML 转换为 C# 代码?

 <ComboBoxItem  x:Name="cmbItemDashDot1">
            <Viewbox>
                <Image  Height="18" Width="70">
                    <Image.Source>
                        <DrawingImage>
                            <DrawingImage.Drawing>
                                <GeometryDrawing Brush="Black">
                                    <GeometryDrawing.Geometry>
                                        <LineGeometry StartPoint="0,9" EndPoint="38,9" />
                                    </GeometryDrawing.Geometry>
                                    <GeometryDrawing.Pen>
                                        <Pen Brush="Black"  Thickness="1"  DashStyle="{x:Static DashStyles.DashDot}"/>
                                    </GeometryDrawing.Pen>
                                </GeometryDrawing>
                            </DrawingImage.Drawing>
                        </DrawingImage>
                    </Image.Source>
                </Image>
            </Viewbox>
        </ComboBoxItem>

我找不到某些元素的类比。 或者如何以编程方式在 ComboBoxItem 中画一条线?

【问题讨论】:

  • 检查您的项目是否引用了System.Windows.Media
  • 找不到类比 - 那是什么?你想念命名空间吗? ComboBoxItemSystem.Windows.Controls.ComboBoxItem(或者只是添加 using System.Windows.Controls;)。如果问题是这样,那么您可以使用 google 查找哪个命名空间是控制(键入“wpf controlname”)。或者只需右键单击代码中的ComboBoxItem 文本并选择Resolve,智能感知将为您修复它。

标签: c# wpf xaml converter


【解决方案1】:

试试这个代码

     Image img = new Image();

     GeometryDrawing gDrwing = new GeometryDrawing();
     gDrwing.Brush = Brushes.Black;

     LineGeometry lineGeo = new LineGeometry();
     lineGeo.StartPoint = new Point(0, 9);
     lineGeo.EndPoint = new Point(38, 9);


     Pen pen = new Pen();
     pen.Brush = Brushes.Black;
     pen.Thickness = 1;
     pen.DashStyle = DashStyles.DashDot;

     gDrwing.Geometry = lineGeo;
     gDrwing.Pen = pen;

     DrawingImage geometryImage = new DrawingImage(gDrwing);

     img.Source = geometryImage;
     Viewbox vb = new Viewbox();
     vb.Child = img;

     comboBox1.Items.Add(vb);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多