Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PropertyChangedEventHandler不工作_C#_Wpf - Fatal编程技术网

C# PropertyChangedEventHandler不工作

C# PropertyChangedEventHandler不工作,c#,wpf,C#,Wpf,我的UserControl xaml中有以下标签和滑块 <Label x:Name="labelValX" Content="{Binding Path=xValue}" HorizontalAlignment="Left" Width="88" Height="44"/> <Slider x:Name="sliderSpeed" Value="{Binding slideValue, Mode=TwoWay}" HorizontalAlignment="Left" Mar

我的UserControl xaml中有以下标签和滑块

<Label x:Name="labelValX" Content="{Binding Path=xValue}" HorizontalAlignment="Left" Width="88" Height="44"/>

 <Slider x:Name="sliderSpeed" Value="{Binding slideValue, Mode=TwoWay}" HorizontalAlignment="Left" Margin="10,35,0,0" VerticalAlignment="Top" Width="173" Height="53" Minimum="10" Maximum="100" />
在我的另一个GetAccNotifications.cs类中,我有以下部分,其中我将xValue字符串定义为特定值:

Y = ((double)(sbyte)value) / 64.0;
Y = Math.Round(Y, 2);
SetGetAccValues set = new SetGetAccValues();
set.xValue = Y.ToString();

当OnPropertyChanged以“xValue”作为propName触发时,问题就会出现,PropertyChangedEventHandler始终保持为null,但当以“slideValue”作为propName触发时,它实际上不为null。为什么在xValue的情况下它仍然为空

我认为您没有绑定DataContext。在您的案例中,应该使用代码隐藏设置DataContext

在SetGetAccValues.xaml.cs中

    public SetGetAccValues()
    {       
        DataContext = this;
    }

我相信PropertyChanged事件是在datacontext加载完成之前触发的

您可以在usercontrol中侦听DataContextChanged事件,以便在新datacontext可用时设置属性

public AccView()
{
    InitializeComponent();

    this.DataContextChanged += OnDataContextChanged;

    this.DataContext = new SetGetAccValues();    
}

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
    Y = ((double)(sbyte)value) / 64.0;
    Y = Math.Round(Y, 2);

    (dependencyPropertyChangedEventArgs.NewValue as SetGetAccValues).xValue = Y.ToString();
}

PropertyChangedEventHandler始终为空是什么意思?是否将事件处理程序连接到PropertyChanged事件?例如:set.PropertyChanged+=SetGetAccValuesPropertyChanged;我的意思是PropertyChanged始终为空,因此我无法访问“if”块。这意味着您没有将事件处理程序连接到SetGetAccValues对象。您可以在创建此对象时发布相关代码吗?您是指GetAccNotifications类中的代码吗?。我只定义了以下几行:SetGetAccValues set=new SetGetAccValues();set.xValue=Y.ToString();正确,请参见下面的答案。我无法移动SetGetAccValues类中的Y值,因为它在GetAccNotifications类中被触发并定义为值。SetGetAccValues如何添加到GetAccNotifications视图中?
public AccView()
{
    InitializeComponent();

    this.DataContextChanged += OnDataContextChanged;

    this.DataContext = new SetGetAccValues();    
}

private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
    Y = ((double)(sbyte)value) / 64.0;
    Y = Math.Round(Y, 2);

    (dependencyPropertyChangedEventArgs.NewValue as SetGetAccValues).xValue = Y.ToString();
}