在Silverlight中的ComboBox中绘制线条

时间:2015-02-16 14:19:07

标签: c# silverlight graphics line

如何在Silverlight中以编程方式在comboBoxItem中创建行? 我想得到这样的东西:
enter image description here

感谢。

我试着这样做:

 var lineTypeComboBox = new ComboBox
        {
            Width = 40,
            Background = Background,
            Margin = new Thickness(0),
            Padding = new Thickness(0)
        };
        lineTypeComboBox.Items.Add(new Line {X1 = 1, X2 = 20, Y1 = 1, Y2 = 20});

1 个答案:

答案 0 :(得分:0)

我从你的代码中看到你没有设置Line的Stroke颜色。尝试将其设置为黑色(并且不要忘记将ComboBox添加到某个容器中):

var lineTypeComboBox = new ComboBox
         {
            Width = 40,
            Background = Background,
            Margin = new Thickness(0),
            Padding = new Thickness(0)
         };
lineTypeComboBox.Items.Add(new Line { X1 = 1, X2 = 20, Y1 = 1, Y2 = 20, Stroke = new SolidColorBrush(Colors.Black) });
//add ComboBox to StackPanel
spMain.Children.Add(lineTypeComboBox);

此外,我并不认为'在Win Forms中编写所有这些代码的好方法。为什么不是XAML,Silverlight的最佳部分之一?哦,你的线坐标有点偏,你可以画对角线。 怎么样?

<ComboBox Width="40">
    <ComboBox.Resources>
        <Style TargetType="Line">
            <Setter Property="X1" Value="1" />
            <Setter Property="Y1" Value="1" />
            <Setter Property="X2" Value="20" />
            <Setter Property="Y2" Value="1" />
            <Setter Property="Stroke" Value="Black" />
        </Style>
    </ComboBox.Resources>

    <ComboBox.Items>
        <ComboBoxItem>
            <Line StrokeThickness="1" />            
        </ComboBoxItem>
        <ComboBoxItem>
            <Line StrokeThickness="3" />            
        </ComboBoxItem>
        <ComboBoxItem>
            <Line StrokeThickness="5" />            
        </ComboBoxItem>
    </ComboBox.Items>
</ComboBox>

您可以直接设置所有属性而不是Style,但灵活性会降低。