CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.02k stars 296 forks source link

使用ObservableProperty特性标注的字段,其更改时无法在WPF控件上显现。 #474

Closed wxysy closed 1 year ago

wxysy commented 1 year ago

Describe the bug

当我使用ObservableProperty特性标注一个字段myString时,该框架自动生成一个对应属性MyString。 将此属性绑定到WPF文本控件TextBox后,我修改字段myString的内容,TextBox控件内容不更新;但是如果我直接修改属性MyString,TextBox控件内容则会自动更新。

Regression

No response

Steps to reproduce

1、环境:.Net 6, WPF框架。IDE:Visual Studio 2022。

2、语言:csharp
[ObservableObject]
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        [RelayCommand]//生成命令SayHelloCommand
        private void SayHello()
        {
            myString = "变化后";    
        }

        [ObservableProperty]
        private string myString = "初始值";
    }

<Window x:Class="ToolkitMVVM.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:ToolkitMVVM"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button x:Name="btn_Action" Command="{Binding Path=SayHelloCommand}" Content="Button" HorizontalAlignment="Left" Margin="49,331,0,0" VerticalAlignment="Top" Height="55" Width="171"/>
        <TextBox x:Name="tb_Show" HorizontalAlignment="Left" Margin="49,37,0,0" TextWrapping="Wrap" Text="{Binding NewString}" VerticalAlignment="Top" Width="273" Height="106" FontSize="18"/>
    </Grid>
</Window>

Expected behavior

请修复此bug。

Screenshots

No response

IDE and version

VS 2022

IDE version

No response

Nuget packages

Nuget package version(s)

8.0.0

Additional context

No response

Help us help you

No, just wanted to report this

Sergio0694 commented 1 year ago

This is by design. You have to set the value of the generated property in order for it to notify changes. If you change that command to set MyString instead of the field, that'll work correctly 🙂

wxysy commented 1 year ago

This is by design. You have to set the value of the generated property in order for it to notify changes. If you change that command to set MyString instead of the field, that'll work correctly 🙂

Thank you for your help.