0

我想以以下行为在我的堆栈面板上显示画笔:

  • 画笔是 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);
 }
4

1 回答 1

1

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

brush.ViewboxUnits = BrushMappingMode.Absolute;
brush.Viewbox = new Rect(0, 30, 240, 60);
于 2013-05-25T20:06:01.037 回答