C# 属性更改时XAML数据绑定不更新UI
在更改整型字段时,我很难更新简单的数据绑定标签。我已经实现了INotifyPropertChanged,当我更改变量值时会触发此事件。UI不会更新,标签也不会更改。我在过去没有做过很多数据绑定,所以我可能遗漏了一些东西,但我还没有找到它 以下是我为XAML准备的内容: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/
<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>