【问题标题】:Fill a rectangle inside button control在按钮控件内填充一个矩形
【发布时间】:2012-06-30 15:45:47
【问题描述】:

我有一个图像(一个框架)和一个矩形(框架内的图片)。我让它们相互匹配,我可以通过代码更改矩形的填充图像,如下所示:

myRectangle.Fill = new ImageBrush(new BitmapImage(new Uri(myImagePath)));

但是我必须为一个按钮制作这两个对象,所以我在 Expression Blend 中选择了它们,右键单击并单击“Make Into Control”并选择“Button”,然后单击“OK”,然后我得到了一个按钮。唯一的问题是我无法填充矩形,因为我不知道如何从代码中获取它。

有没有人可以告诉我如何从按钮内的代码到达我的矩形,并用图像填充它?

【问题讨论】:

  • 按钮控件的 Xaml 是什么样的。

标签: c# .net wpf wpf-controls expression-blend


【解决方案1】:

如果您知道控件的名称,例如按钮,您应该能够在代码隐藏中找到它,如下所示

var myRectangleButton = (Button)this.FindName("myRectangleButton");

【讨论】:

    【解决方案2】:

    像你一样填充矩形,然后:

    button.Content = myRectangle;
    

    在 XAML 中:

    <Button>
      <Button.Content>
         <Rectangle Width="100" Height="50">
            <Rectangle.Fill>
               <SolidColorBrush Color="Aqua" />
             </Rectangle.Fill>
          </Rectangle>
      </Button.Content>
    </Button>
    

    参考:Answer link

    您可以进一步找到 ContentControl.Content 属性Here的示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多