Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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# 属性更改时XAML数据绑定不更新UI_C#_Wpf_Xaml_Data Binding_Inotifypropertychanged - Fatal编程技术网

C# 属性更改时XAML数据绑定不更新UI

C# 属性更改时XAML数据绑定不更新UI,c#,wpf,xaml,data-binding,inotifypropertychanged,C#,Wpf,Xaml,Data Binding,Inotifypropertychanged,在更改整型字段时,我很难更新简单的数据绑定标签。我已经实现了INotifyPropertChanged,当我更改变量值时会触发此事件。UI不会更新,标签也不会更改。我在过去没有做过很多数据绑定,所以我可能遗漏了一些东西,但我还没有找到它 以下是我为XAML准备的内容: <Window x:Class="TestBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/

在更改整型字段时,我很难更新简单的数据绑定标签。我已经实现了INotifyPropertChanged,当我更改变量值时会触发此事件。UI不会更新,标签也不会更改。我在过去没有做过很多数据绑定,所以我可能遗漏了一些东西,但我还没有找到它

以下是我为XAML准备的内容:

         <Window x:Class="TestBinding.MainWindow" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                xmlns:mc="http://schemas.openxmlformats.org/markup-
                compatibility/2006"
                xmlns:local="clr-namespace:TestBinding"
                mc:Ignorable="d"
                Title="MainWindow" Height="350" Width="525">
            <StackPanel>
                <Button Command="{Binding TestCommand, Mode=OneWay}" >
                    <Button.DataContext>
                        <local:TestVM/>
                    </Button.DataContext> Add 1</Button>
                <Label Content="{Binding Count,
                 Mode=OneWay,UpdateSourceTrigger=PropertyChanged}">
                    <Label.DataContext>
                        <local:TestVM/>
                    </Label.DataContext>
                </Label>
            </StackPanel>
        </Window>
这是我的ICommand C#(以防您需要它来复制我所拥有的):

公共类RelayCommand:ICommand
{
#区域字段
只读操作_执行;
只读谓词_canExecute;
#endregion//字段
#区域构造函数
public RelayCommand(Action execute):这个(execute,null){}
公共RelayCommand(操作执行,谓词canExecute)
{
如果(execute==null)抛出新的ArgumentNullException(“execute”);
_执行=执行;
_canExecute=canExecute;
}
#endregion//构造函数
#区域ICommand成员[DebuggerStepThrough]
公共布尔CanExecute(对象参数)
{
返回_canExecute==null?true:_canExecute(参数);
}
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
public void Execute(对象参数)
{
_执行(参数);
}
#端区
//I命令成员
}
非常感谢您的任何帮助

编辑: 我根据toadflakz的建议更新了我的xaml,它成功了

<Window x:Class="TestBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:TestVM/>
    </Window.DataContext>
    <StackPanel>
    <Button Command="{Binding TestCommand, Mode=OneWay}" >Add 1</Button>
    <Label Content="{Binding Count, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Window>

加1

您的问题在于如何将DataContext绑定到各个控件。ViewModel不会自动成为一个单实例对象(仅限于单个实例),因此每次指定它时,实际上都是在创建一个单独的实例


如果在窗口级别设置DataContext,则代码应按预期工作。

窗口的
DataContext
设置为:

<Window......
    <Window.DataContext>
         <local:TestVM/>
    </Window.DataContext>
</Window>

<Window x:Class="TestBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:TestVM/>
    </Window.DataContext>
    <StackPanel>
    <Button Command="{Binding TestCommand, Mode=OneWay}" >Add 1</Button>
    <Label Content="{Binding Count, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Window>
<Window......
    <Window.DataContext>
         <local:TestVM/>
    </Window.DataContext>
</Window>