INotifyPropertyChanged 只获取值但不设置值

问题描述 投票:0回答:1

我在这里检查了有关此问题的许多线程,但没有找到适合我的特定问题的解决方案 案件。 有一个基于 WPF c# Net 6 的项目。在此有一个用于获取/设置参数的模态对话框窗口。 从对话框获取有效,但设置无效。 xaml代码:

<Window x:Class="MyApp.ParamDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyApp"
    Title="Instrument Properties Dialog" Height="450" Width="400">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <DockPanel Grid.Row="0" LastChildFill="true">
        <Label  Content="int property" Margin="0,5,0,0" Width="130"/>
        <TextBox  x:Name="myIntProperty" TextWrapping="NoWrap" TextAlignment="Center" HorizontalAlignment="Stretch" Margin="5" Text="{Binding myIntProperty,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, IsAsync=true}" />
    </DockPanel>

    <!-- Buttons -->
    <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,0">
        <Button Content="OK" Width="80" Height="25" Margin="5" Click="OKButton_Click"/>
        <Button Content="Cancel" Width="80" Height="25" Margin="5" Click="CancelButton_Click"/>
    </StackPanel>
</Grid>

这是c#代码:

using System.Windows;

namespace MyApp {
using System.ComponentModel;

#region class DataContextParamDlg
public class DataContextParamDlg : INotifyPropertyChanged {
    private int _myIntProperty = 100;

    public event PropertyChangedEventHandler PropertyChanged;

    public virtual void OnPropertyChanged(string propertyName) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public int myIntProperty {
        get { return _myIntProperty; }
        set {
            _myIntProperty = value;
            OnPropertyChanged("myIntProperty");
        }
    }
}
#endregion

public partial class ParamDialog : Window {

    private DataContextParamDlg _DataContext;

    public ParamDialog() {
        InitializeComponent();

        _DataContext = new DataContextParamDlg();
        DataContext = _DataContext;

        // works not -> not shown in text box ###problem###
        _DataContext.myIntProperty = 2;
    }

    public DataContextParamDlg DataContext {
        get; private set;
    }

    private void OKButton_Click(object sender, RoutedEventArgs e) {
        DialogResult = true;

        // this is fine. output carries value typed in TextBox
        int output = _DataContext.myIntProperty;
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e) {
        DialogResult = false;
    }
}

}

最后提出对话:

ParamDialog dlg = new ParamDialog();
bool? result = dlg.ShowDialog();

如何解决###问题###? 非常感谢

c# wpf
1个回答
0
投票

此代码也只是获取而不设置。

using System.Windows;

namespace MyApp {
using System.ComponentModel;


public partial class ParamDialog : Window {


    public ParamDialog() {
        InitializeComponent();


        // works not -> not shown in text box
        myIntProperty.Text = "2";

    }

  

    private void OKButton_Click(object sender, RoutedEventArgs e) {
        DialogResult = true;

        // this is fine. output has value of TextBox
        int output = System.Convert.ToInt32(myIntProperty.Text);
    }

    private void CancelButton_Click(object sender, RoutedEventArgs e) {
        DialogResult = false;
    }
}

}

© www.soinside.com 2019 - 2024. All rights reserved.