在Stackpanel上显示画笔的一部分

时间:2013-05-25 19:16:09

标签: c# wpf visible stackpanel brush

我想在我的堆栈面板上显示一个画笔,行为如下:

  • 画笔是图像240x120
  • 面板是240x60

我想显示画笔的一部分,如rect(0,30,240,60)(以便面板上的图像向下移动一点) 尝试使用视口和Viewbox没有结果(空面板)

这是我的代码:

for (int i = 0; i < listExplorationData.Count; i++)
{ 
    StackPanel panelLoop = new StackPanel();
    panelLoop.Name = "panel_" + i.ToString();
    panelLoop.Width = 240;
    panelLoop.Height = 60;
    panelLoop.Margin = new Thickness(0, 60 * i, 0, 0);

    BitmapImage image = new BitmapImage(
        new Uri("pack://application:,,,/GW2-MyWorldExploration;component/Images/" +
                listExplorationData[i].mapname_en.Replace(" ", "_") +
                "_loading_screen.jpg"));
    ImageBrush brush = new ImageBrush();
    brush.ImageSource = image;
    brush.Stretch = Stretch.None;
    brush.Viewport = new Rect(0, 30, 240, 60);
    panelLoop.Background = brush;
    mainStackPanel.Children.Add(panelLoop);
 }

1 个答案:

答案 0 :(得分:1)

为了显示ImageBrush的一部分,您必须设置Viewbox。如果要以绝对单位指定视图框,则还必须设置ViewboxUnits属性:

brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.Viewbox = new Rect(0, 30, 240, 60);