【问题标题】:WPF button with image user control in design mode设计模式下带有图像用户控件的 WPF 按钮
【发布时间】:2012-11-19 07:51:39
【问题描述】:

我做了一个这样的简单用户控件,以支持按钮内的图像和文本:

<UserControl x:Class="wpf_Templates.UC.rb"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         DataContext="{Binding RelativeSource={RelativeSource Self}}" >
<Grid>
    <Button Name="rbutton1">
        <StackPanel Orientation="{Binding Layout}" Background="Beige">
            <Image Source="{Binding Image}" />
            <TextBlock Text="{Binding Text}" Margin="3,3,0,0" HorizontalAlignment="Center"/>
        </StackPanel>

    </Button>
</Grid>

它有两个 DependencyProperty : Text , Image。

在 MainWindow.xaml 我这样实例化它

 <lcl:rb Text="Rb" Image="Resources\Images\Exit.gif" MinWidth="40"></lcl:rb>

现在当应用程序运行时,我确实看到了带有文本和图像的按钮, 但我在设计模式下看到空框。 为什么?

谢谢,阿维。

【问题讨论】:

    标签: wpf image button


    【解决方案1】:

    在设计时路径略有不同,因为它本地化到页面。

    如果用户控件位于子目录文件夹中,则绑定到路径Resources\Images\Exit.gif 将失败,因为设计器位于没有Resources 子文件夹的控件 目录中。

    如果您添加..\ 表示从子文件夹访问的路径,作为测试,到..\Resources\Images\Exit.gif,它可能会使其在页面的设计模式下工作,但可能在运行时失败。

    无论如何,您都可以决定如何在设计模式下使用图像。

    【讨论】:

      【解决方案2】:

      您可以在 MainWindow.xaml 中的 Window 声明中添加以下属性:

      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d" 
      

      在你的控制声明中:

      <lcl:rb Text="Rb" 
              Image="Resources\Images\Exit.gif" 
              d:Image="Resources\Images\Exit.gif"  
              MinWidth="40">
      </lcl:rb>
      

      这样设计师就知道要使用什么了。

      【讨论】:

      • 或者按照我的建议尝试d:Image="..\Resources\Images\Exit.gif",但使用马修的回答。
      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2017-10-08
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2011-02-11
      • 2016-01-15
      相关资源
      最近更新 更多