WPF UserControl内容在设计模式下不显示

时间:2020-10-27 14:42:43

标签: wpf-controls dependency-properties

我有UserControl作为按钮,需要显示图像和文本。当我运行应用程序时,一切正常,但在设计器中文本和图像未显示。 UserControl所在的项目位于同一项目中

XAML

<UserControl x:Class="ClientHCS.controls.ucBtn"
         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" 
         xmlns:local="clr-namespace:ClientHCS.controls"
         mc:Ignorable="d" 
         d:DesignHeight="35" d:DesignWidth="100">
    <Button Name="Btn"  Click="Btn_Click"  Padding="0">
        <StackPanel Orientation="Horizontal" >
            <Image Name="ImBtn" />
            <Label Name="lblContent"/>
        </StackPanel>
    </Button>
</UserControl>

C#

public static DependencyProperty ImgSourceProperty = DependencyProperty.Register("ImgSource", typeof(ImageSource), typeof(ucBtn)
        , new UIPropertyMetadata(
           default(ImageSource), new PropertyChangedCallback(OnImgSourceChanged))
        );

    public ImageSource ImgSource {
        get { return (ImageSource)GetValue(ImgSourceProperty); }
        set { SetValue(ImgSourceProperty, value); 
        }
    }
    private static void OnImgSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        ucBtn ctrl = (ucBtn)d;
        ctrl.ImBtn.Source = (ImageSource)e.NewValue;

    }

我将此控件添加到结果表单中

<controls:ucBtn DockPanel.Dock="Top" HorizontalAlignment="Center" x:Name="btnClose" Margin="140,5,59,5" Click="btnClose_Click" BtnContent="Close" ImgSource='\icons\Close.ico' Width="93" Height="19"/>

我添加了DependencyProperty,设置了PropertyChangedCallback,但在Studio构造函数中看到了空按钮。

0 个答案:

没有答案