Wpf 依赖项属性不工作,正在尝试通过样式设置器进行设置
我正在尝试为新创建的usercontrol设置自定义样式,但遇到错误:“无法将属性'Property'中的值转换为'System.Windows.DependencyProperty'类型的对象。” 我以为我已经设置了依赖属性,但似乎不是这样,所以我做了一些研究,并补充说:Wpf 依赖项属性不工作,正在尝试通过样式设置器进行设置,wpf,xaml,styles,dependency-properties,Wpf,Xaml,Styles,Dependency Properties,我正在尝试为新创建的usercontrol设置自定义样式,但遇到错误:“无法将属性'Property'中的值转换为'System.Windows.DependencyProperty'类型的对象。” 我以为我已经设置了依赖属性,但似乎不是这样,所以我做了一些研究,并补充说: public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.Register("ImageSource"
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(Image));
为此:
--MyButton.Xaml.Cs--
不幸的是,这不起作用。我也试过:
public BitmapSource ImageSource
{
get { return (BitmapSource)GetValue(MyButton.ImageSourceProperty); }
set
{
SetValue(ImageSourceProperty, value);
}
}
但这不起作用,图像也没有显示,并且产生了与前面提到的相同的错误
有什么想法吗?
问候科汉
--MyButton.Xaml--
--我的按钮样式--
我看到的最大问题是,您将该财产注册为
Image
所有,而不是UserControl
所有。改为:
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(BitmapSource), typeof(MyButton));
如果不起作用,将需要查看您的XAML。为您的UserControl设置DataContext:
public MyButton()
{
InitializeComponent();
DataContext = this;
}
或者,如果您不能这样做(例如,因为DataContext被设置为另一个对象),您可以在XAML中这样做:
<UserControl x:Class="Client.Usercontrols.MyButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinHeight="30" MinWidth="40"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
x:Name="MyControl">
<Button Width="Auto" HorizontalAlignment="Center" Click="onButtonClick">
<Border CornerRadius="5" BorderThickness="1" BorderBrush="Transparent" >
<Grid>
<Image Name="tehImage" Source="{Binding ElementName=MyControl, Path=ImageSource}" />
<TextBlock Name="tehText" Text="{Binding ElementName=MyControl, Path=Text}" Style="{DynamicResource ButtonText}" />
</Grid>
</Border>
</Button>
</UserControl>
依赖项属性的标准格式是(我在您的信息中添加了): 看起来您也在尝试将dependency属性传递给名为“tehImage”的对象的ImageSource。您可以使用PropertyChangedCallback将其设置为自动更新。。。这意味着无论何时更新属性,都会自动调用更新 因此,属性代码变为:
public BitmapSource ImageSource
{
get { return (BitmapSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
/* Using a DependencyProperty as the backing store for ImageSource.
This enables animation, styling, binding, etc... */
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource",
typeof(BitmapSource), typeof(MyButton),
new UIPropertyMetadata(null,
ImageSource_PropertyChanged
)
);
private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
((MyButton)source).tehImage.ImageSource = (ImageSource)e.NewValue
}
希望通过正确注册的依赖属性,这将帮助您缩小问题范围(甚至解决问题)我认为在用户控件中实现图像源的正确方法不是BitmapSouce。最简单和最好的方法(我再次表示)是使用Uri 将依赖项属性更改为此(同时定义更改回调事件): 以及本协议的财产:
public Uri ImageSource
{
get
{
return (Uri)GetValue(ImageSourceProperty);
}
set
{
SetValue(ImageSourceProperty, value);
}
}
您的回电是这样的:
private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
MyButton hsb = (MyButton)sender;
Image image = hsb.tehImage;
image.Source = new BitmapImage((Uri) e.NewValue);
}
这似乎已经阻止了错误在我的样式中发生,但是现在它停止了我的usercontrol的工作,它已经从所有使用它的地方消失了。我已经添加了usercontrol的Xaml。谢谢,我想我快到了。我不得不修改ImageSource_属性以使其得以构建,也许这就是它出错的地方?但是当我的按钮被设置为“禁用”时,我的样式(我现在在上面添加了样式以查看)不适用于我的按钮。有什么想法吗?私有静态void ImageSource_属性已更改(DependencyObject源,DependencyPropertyChangedEventArgs e){((MyButton)源)。tehImage.source=(ImageSource)e.NewValue;}
public BitmapSource ImageSource
{
get { return (BitmapSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
/* Using a DependencyProperty as the backing store for ImageSource.
This enables animation, styling, binding, etc... */
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource",
typeof(BitmapSource),
typeof(MyButton),
new UIPropertyMetadata(null)
);
public BitmapSource ImageSource
{
get { return (BitmapSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
/* Using a DependencyProperty as the backing store for ImageSource.
This enables animation, styling, binding, etc... */
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource",
typeof(BitmapSource), typeof(MyButton),
new UIPropertyMetadata(null,
ImageSource_PropertyChanged
)
);
private static void ImageSource_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
((MyButton)source).tehImage.ImageSource = (ImageSource)e.NewValue
}
ImageSourceProperty = DependencyProperty.Register(
"ImageSource", typeof (Uri), typeof (MyButton),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnImageSourceChanged)));
public Uri ImageSource
{
get
{
return (Uri)GetValue(ImageSourceProperty);
}
set
{
SetValue(ImageSourceProperty, value);
}
}
private static void OnImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
MyButton hsb = (MyButton)sender;
Image image = hsb.tehImage;
image.Source = new BitmapImage((Uri) e.NewValue);
}