C# 获取属性值WPF

C# 获取属性值WPF,c#,wpf,xaml,C#,Wpf,Xaml,我试图从属性中获取值,但不起作用。我总是获取空值 string imageNormal; public static readonly DependencyProperty ImageNormalProperty = DependencyProperty.Register("ImageNormal", typeof(string), typeof(MainWindow)); public string ImageNormal { get { r

我试图从属性中获取值,但不起作用。我总是获取空值

string imageNormal;

    public static readonly DependencyProperty ImageNormalProperty =
        DependencyProperty.Register("ImageNormal", typeof(string), typeof(MainWindow));

public string ImageNormal
    {
        get { return (string)GetValue(ImageNormalProperty); }
        set { SetValue(ImageNormalProperty, value); }
    }

public ButtonImageStyle()
    {
        InitializeComponent();
        DataContext = this;
        Console.WriteLine("Path: " + ImageNormal);
    }
Xaml ButtonImageStyle.Xaml:

<Image Source="{Binding ImageNormal}" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />

Xaml MainWindow.Xaml:

<local:ButtonImageStyle HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="88" ImageNormal="C:/Users/Xafi/Desktop/add.png"/>

我总是获得下一个输出:
路径:

因为您的ImageSource必须绑定到它的父依赖属性(定义到您的代码后面),所以您必须将绑定定义为相对于您的UserControl(让我们将其命名为)。因此,请尝试用下一种方法更改xaml:

Xaml代码

<UserControl x:Class="SomeBindingExampleSOHelpAttempt.ButtonImageStyle"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" x:Name="This">
<Grid>
    <Image Source="{Binding ElementName=This, Path=ImageNormal, UpdateSourceTrigger=PropertyChanged}" 
           Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid></UserControl>


注意。

因为您的ImageSource必须绑定到它的父依赖属性(定义到您的代码后面),所以您必须将绑定定义为相对于您的UserControl(让我们命名它)。因此,请尝试用下一种方法更改xaml:

Xaml代码

<UserControl x:Class="SomeBindingExampleSOHelpAttempt.ButtonImageStyle"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" x:Name="This">
<Grid>
    <Image Source="{Binding ElementName=This, Path=ImageNormal, UpdateSourceTrigger=PropertyChanged}" 
           Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid></UserControl>


问候。

您是否将任何内容绑定到此属性?你把它放在哪里?为什么顶部有一个孤立的
字符串imageNormal
浮动?@axlj是的,我知道了——谢谢,那么imageNormal路径分配到哪里呢?根据上面的代码,从未分配属性?在写入
DataContext=this
时,您正在硬编码DataContext,因此无法将任何值传递到UserControl。删除该行,它可能会像您预期的那样工作。我正在执行一个自定义按钮,我想保存两个状态,然后我正在测试如何保存字符串属性@Rachel I deleted,但仍然无法使用axlj I assing designer的属性您是否将任何内容绑定到此属性?你把它放在哪里?为什么顶部有一个孤立的
字符串imageNormal
浮动?@axlj是的,我知道了——谢谢,那么imageNormal路径分配到哪里呢?根据上面的代码,从未分配属性?在写入
DataContext=this
时,您正在硬编码DataContext,因此无法将任何值传递到UserControl。删除该行,它可能会像您预期的那样工作。我正在执行一个自定义按钮,我想保存两个状态,然后我正在测试如何保存字符串属性@Rachel I deleted,但仍然不工作axlj我正在从设计器中分配属性,但我的目标是从代码中获取属性值,以及,从设计器i.gyazo.com/6ab8172860b0544cc5d81a89a3f0d9c6.png中介绍它,但我的目标是从代码中获取属性值,以及这些值,从设计器i.gyazo.com/6ab8172860b0544cc5d81a89a3f0d9c6.png中介绍它