WPF C#自定义控件无法访问我的项目中的CustomImageButton类

时间:2015-05-07 12:51:58

标签: c# wpf xaml custom-controls

我有一个WPF C#解决方案Main,在那个解决方案中我有多个rpojects。

解决方案:主要

- Project: Common
- Project: WindowThemes
              - Themes => Generic.xaml
              - CustomImageButton.cs
- Project: StartUp
- Project: UI
              - User Control : User.xaml

在我的User.xaml中,我正在尝试附加我在WindowThemes项目中创建的自定义控件。

CustomImageButton.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

public class CustomImageButton : DependencyObject
{
    #region Image Dependency Property
    /// <summary>
    /// An attached dependency property which prides an
    /// <see cref="ImageSource"/> for arbitrary WPF elements
    /// </summary>
    public static readonly DependencyProperty ImageProperty;

    /// <summary>
    /// Gets the <see cref="ImageProperty"/> for a given
    /// <see cref="DependencyObject"/>, which provides an
    /// <see cref="ImageSource"/> for arbitrary WPF elements
    /// </summary>
    public static ImageSource GetImage(DependencyObject obj)
    {
        return (ImageSource)obj.GetValue(ImageProperty);
    }

    /// <summary>
    /// Gets the attached <see cref="ImageProperty"/> for a given
    /// <see cref="DependencyObject"/>, which provides an
    /// <see cref="ImageSource" /> for arbitrary WPF elements.
    /// </summary>
    public static void SetImage(DependencyObject obj, ImageSource value)
    {
        obj.SetValue(ImageProperty, value);
    }
    #endregion

    static CustomImageButton()
    {
        var metadata = new FrameworkPropertyMetadata((ImageSource)null);
        ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(CustomImageButton), metadata);
    }
}

Generic.xaml:

<!-- Custom Button Image -->
<Style TargetType="{x:Type Button}" x:Key="CustomImageButtonStyle">
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Image Source="{Binding Path=(local:CustomImageButton.Image), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
                           HorizontalAlignment="Left"
                           Margin="8, 0, 0, 0"
                           Height="16"
                           Width="16"/>

                    <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center"/>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

直到现在一切都很好Itès当我想将这个自定义模板与我的用户控件中的按钮关联时,它不会允许我......

User.xaml:

<UserControl x:Class="UI.User.User"
         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:Themes="clr-namespace:WindowThemes"
         mc:Ignorable="d" >

<Grid Name="grdMain">
    <StackPanel Name="spUserInfo" Orientation="Horizontal" Margin="5">
        <Button x:Name="btnLogOut" Themes:CustomImageButton.Image="/Common.Library;component/Images/Ok.png" Content="Ok" Style="{DynamicResource CustomImageButtonStyle}"/>
        <TextBlock Name="tbFullName" VerticalAlignment="Center" Margin="0, 0, 20, 0" FontSize="32" Foreground="#EA1E63"/>
        <Image Name="imgUserPicture" Source="/Common.Library;component/Images/pwd_ico.png" VerticalAlignment="Center"/>
    </StackPanel>
</Grid>

Itès给我一个错误:

Themes:CustomImageButton.Image="/Common.Library;component/Images/Ok.png" Content="Ok"

错误:名称'CustomImageButton'在命名空间'clr-namespace中不存在:WindowThemes'

我做错了什么?

1 个答案:

答案 0 :(得分:2)

我认为您的问题是您的CustomImageButton类位于项目WindowThemes中,但不在此命名空间中。您必须将您的类放入此命名空间:

namespace WindowThemes
{
    public class CustomImageButton : DependencyObject
    {
       ...
    }
}