C# 图像资源作为不显示图像的源

C# 图像资源作为不显示图像的源,c#,wpf,image,C#,Wpf,Image,我在WPF UserControl库中定义了一个自定义加载微调器UserControl。 它有一个依赖属性: public string SpinnerSourcePath { get => _spinner.Source.ToString(); set => _spinner.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(value); } public static readonly Dep

我在WPF UserControl库中定义了一个自定义加载微调器UserControl。 它有一个依赖属性:

public string SpinnerSourcePath { get => _spinner.Source.ToString(); set => _spinner.Source = (ImageSource)new ImageSourceConverter().ConvertFromString(value); }

public static readonly DependencyProperty SpinnerSourcePathProperty =
            DependencyProperty.Register(nameof(SpinnerSourcePath), typeof(string), typeof(Spinner));

其中
\u微调器
图像

(我直接用
ImageSource
类进行了尝试,但没有骰子)

xaml如下所示:

<Image x:Name="_spinner" RenderTransformOrigin="0.5 0.5">
    <SomeStyleToMakeItRotate.../>
</Image>
<Image Source="{Binding SpinnerSource,
                RelativeSource={RelativeSource AncestorType=UserControl}}"/>
它正确地显示了

这让我相信依赖性属性是错误的,但是如何呢

E1: 在另一个控件上尝试执行相同步骤后,它再次停止工作

这次我有一个DP:

public static readonly DependencyProperty ValidationFunctionProperty =
    DependencyProperty.Register(nameof(ValidationFunction), typeof(Func<string, bool>), typeof(ValidatedTextBox), new PropertyMetadata(OnAssignValidation));

public Func<string, bool> ValidationFunction {
    get => (Func<string, bool>)GetValue(ValidationFunctionProperty);
    set => SetValue(ValidationFunctionProperty, value);
}

private static void OnAssignValidation(DependencyObject d, DependencyPropertyChangedEventArgs e) {
    Debugger.Break();
}
(文本
也有问题,但我想我可以自己解决)

E2
Ok Pro的问题是
相对路径
指向
用户控件
,但它被放置在
窗口中

您的依赖项属性声明错误,因为CLR属性包装器的
get
/
set
方法必须调用DependencyObject基类的
GetValue
SetValue
方法(而不是其他方法)

public MainWindow() {
    InitializeComponent();
}

public Func<string,bool> Validation => (s) => true;
除此之外,属性还应使用
ImageSource
作为其类型:

public static readonly DependencyProperty SpinnerSourceProperty =
    DependencyProperty.Register(
        nameof(SpinnerSource), typeof(ImageSource), typeof(Spinner));

public ImageSource SpinnerSource
{
    get { return (ImageSource)GetValue(SpinnerSourceProperty); }
    set { SetValue(SpinnerSourceProperty, value); }
}
UserControl的XAML中的Image元素将使用如下属性:

<Image x:Name="_spinner" RenderTransformOrigin="0.5 0.5">
    <SomeStyleToMakeItRotate.../>
</Image>
<Image Source="{Binding SpinnerSource,
                RelativeSource={RelativeSource AncestorType=UserControl}}"/>

哦,我不知道您可以像这样绑定依赖项属性。酷,现在它正在工作。