WPF 绑定仅在 xaml 热重载修改后应用

WPF binding is only applied after xaml hot reload modification

我有一个奇怪的问题。 我在 VS19 上工作,我有绑定,只有当我在启动后对 xaml 文件进行更改时它们才有效……我认为数据网格内容没有很好地刷新,但我不知道为什么。

操作:

  1. 我执行程序。
  2. 在数据网格中,用户修改单元格 1 绑定到 属性 A(双向模式,在失去焦点上,因为转换器必须首先完成它的工作)。
  3. 失去焦点事件在属性A的基础上修改了属性B(我检查值是否已更改)
  4. 单元格 2 绑定到 属性 B(双向模式 属性更改)但数据网格没有更改...
  5. 我修改 xaml 文件(我可以更改一些内容并再次还原此更改)单元格 2 现在有 属性 B

为了检查值是否绑定良好,我添加了一个 DataGridTextColumn。该值仅在我进入此单元格的编辑模式后可见。

所以这里有一些代码:

xaml :

                  <DataGrid x:Name="DT" AutoGenerateColumns="False" IsEnabled="True" LostFocus="DT_LostFocus">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Date" Width="Auto"  Binding="{Binding StringPropertyA, Mode=TwoWay, Converter={StaticResource dateChecker}}"/>
                        <DataGridTextColumn Header="MonthSimple" Width="Auto"  Binding="{Binding StringPropertyB, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}"/>
                        <DataGridTemplateColumn Header="Month">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox SelectedValue="{Binding StringPropertyB, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                               SelectedItem="{Binding StringPropertyB, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                           ItemsSource="{Binding ElementName=Combobox, Path=ItemsSource}" Style="{DynamicResource ComboBoxStyle1}">
                                    </ComboBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>                           
                    </DataGrid.Columns>
                </DataGrid>

背后的代码:

        private void DT_LostFocus(object sender, RoutedEventArgs e)
    {
        foreach (CustomClass custom in DT.ItemsSource)
        {
            if ((custom.StringPropertyA == "" || custom.StringPropertyA == null) && DateTime.TryParse(custom.StringPropertyA , out DateTime date))
            {
                custom.StringPropertyB = OtherOperations(date);
            }
        }
    }

谁能发现我的错误?或者告诉我为什么绑定似乎不起作用?

这里有一些适合我的更改。正如@Steeeve 所建议的,我将逻辑移到了 setter:

所以 XAML:

              <DataGrid x:Name="DT" AutoGenerateColumns="False" IsEnabled="True">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Date" Width="Auto"  Binding="{Binding StringPropertyA, Mode=TwoWay, Converter={StaticResource dateChecker}}"/>
                    <DataGridTextColumn Header="MonthSimple" Width="Auto"  Binding="{Binding StringPropertyB, Mode=TwoWay , UpdateSourceTrigger=PropertyChanged}"/>
                    <DataGridTemplateColumn Header="Month">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox SelectedValue="{Binding StringPropertyB, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                           SelectedItem="{Binding StringPropertyB, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                       ItemsSource="{Binding ElementName=Combobox, Path=ItemsSource}" Style="{DynamicResource ComboBoxStyle1}">
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>                           
                </DataGrid.Columns>
            </DataGrid>

背后的代码:

public class CustomClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 
    public string StringPropertyA { get; set; }
    private string _stringPropertyB;
    public string StringPropertyB
    {
        get { return _stringPropertyB; }
        set
        {
            if ((this.StringPropertyA == "" || this.StringPropertyA == null) && DateTime.TryParse(value, out DateTime date))
            {
                this.StringPropertyB = OtherOperations(date);
                OnPropertyChanged();
                _stringPropertyB = value;
            }
            else
            {
                _stringPropertyB= value;
                OnPropertyChanged();
            }
        }
    }

    protected void OnPropertyChanged(string name = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

绑定始终是最新的。 感谢@Steeeve 的建议!