WPF C# 自定义控件无法访问我项目中的 CustomImageButton class
WPF C# Custom Control Cannot Acces to my CustomImageButton class in my project
我有一个 WPF C# Solution 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>
直到现在一切都很好当我想将这个自定义模板关联到我的用户控件中的按钮时,它不允许我...
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>
它给我一个错误:
Themes:CustomImageButton.Image="/Common.Library;component/Images/Ok.png" Content="Ok"
错误:名称 'CustomImageButton' 在命名空间 'clr-namespace:WindowThemes'
中不存在
我做错了什么?
我认为你的问题是你的 CustomImageButton
class 在项目 WindowThemes
中但不在这个命名空间中。您必须将 class 放入此命名空间:
namespace WindowThemes
{
public class CustomImageButton : DependencyObject
{
...
}
}
我有一个 WPF C# Solution 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>
直到现在一切都很好当我想将这个自定义模板关联到我的用户控件中的按钮时,它不允许我...
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>
它给我一个错误:
Themes:CustomImageButton.Image="/Common.Library;component/Images/Ok.png" Content="Ok"
错误:名称 'CustomImageButton' 在命名空间 'clr-namespace:WindowThemes'
中不存在我做错了什么?
我认为你的问题是你的 CustomImageButton
class 在项目 WindowThemes
中但不在这个命名空间中。您必须将 class 放入此命名空间:
namespace WindowThemes
{
public class CustomImageButton : DependencyObject
{
...
}
}