WPF按钮,在设计模式下具有图像用户控件

时间:2012-11-19 07:51:39

标签: wpf image button

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

<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>

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

谢谢,Avi。

2 个答案:

答案 0 :(得分:1)

在设计时,路径有点不同,因为已本地化到页面

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

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

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

答案 1 :(得分:0)

您可以将以下属性添加到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>

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

相关问题