【问题标题】:Textbox not updating文本框不更新
【发布时间】:2023-03-16 14:31:01
【问题描述】:

我尝试过搜索,但我可能没有使用正确的术语进行搜索。

我有几个我正在使用的文本框,当我输入数据时,我看到调试时更新的值,但它们从未更新到表单。

基本上,我有一个用于视觉效果的表单,然后我有一个处理所有活动的类。我已经将该类创建为资源,并且我正在引用文本框中的资源。

我真正知道如何处理表单更新的唯一方法是实现对值变化的计算。因此,如果我更新了一个属性,我会从 OnPropertyChanged() 调用该方法。这会产生问题,因为由于计算重写值,值总是会发生变化。然后我尝试评估新值的变化

I.E.

public double In1
{
    get{_return _in1;}
    set{
        if (_in1 != value)
        _in1 = value;

        OnPropertyChanged("In1");
    }
}

无论如何,我的问题是我没有看到值被写入文本框。这是我第一次真正尝试使用数据绑定,所以我假设我做错了(很明显)

<ad:DockableContent
    xmlns="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" 
    x:Class="DMC_Robot_Editor.GUI.frmAngleConvertor"
    Title="frmAngleConvertor" Height="259" Width="282">
<ad:DockableContent.Resources>       
    <local:AngleConvertor x:Key="Converter"/>
</ad:DockableContent.Resources>
<Grid >    
        <GroupBox HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <Grid>
 <ComboBox  x:Name="cbInput" HorizontalAlignment="Stretch" VerticalAlignment="Top"     Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1"  DisplayMemberPath="ValueCartesianString"     SelectedValuePath="ValueCartesianEnum" IsSynchronizedWithCurrentItem="True"      SelectedIndex="{Binding InputItem,Source={StaticResource Converter}}" ItemsSource="{Binding InputConvention, Source={StaticResource Converter}}"  IsReadOnly="True"/>
                    <TextBox x:Name="tbIn1" HorizontalAlignment="Center"     VerticalAlignment="Bottom" Text="{Binding In1, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}" Grid.Column="0"     d:LayoutOverrides="GridBox" Grid.Row="2" Width="50" TextAlignment="Center">
                        <TextBox.DataContext>
                            <local:AngleConvertor/>
                        </TextBox.DataContext>                    </Grid>
</ad:DockableContent>

public class AngleConverter()
{
    private double _in1 = 0.0;
    public double In1
    {
        get{_return _in1;}
        set{
            if (_in1 != value)
            _in1 = value;

            OnPropertyChanged("In1");
        }
    }
}

【问题讨论】:

    标签: c# wpf data-binding inotifypropertychanged


    【解决方案1】:

    尝试在您的文本框中应用UpdateSourceTrigger=PropertyChanged

    Text="{Binding Path-In1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}"  
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    【讨论】:

      【解决方案2】:

      您可以使用您的模式 TwoWay 添加到您的绑定 UpdateSourceTrigger=PropertyChanged

      <TextBox Name="tbIn1" 
      
      HorizontalAlignment="Center"     
      VerticalAlignment="Bottom" 
      
      Text="{Binding In1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, 
      Converter={StaticResource DoubleToStringConverter}, 
      Source={StaticResource Converter}}" 
      
      Grid.Column="0"     
      d:LayoutOverrides="GridBox" 
      Grid.Row="2" 
      Width="50" 
      TextAlignment="Center"
      /> 
      

      【讨论】:

        【解决方案3】:

        你的真实代码应该是这样的:

        public class AngleConverter : INotifyPropertyChanged
        

        所以我认为这只是您的代码中的一个错字。你没有发布你的转换器代码,你的文本框中的绑定是好的。文本框默认 UpdateSourceTrigger 是 lostfocus。所以也许 UpdateSourceTrigger=PropertyChanged 做了你想要的。

        【讨论】:

          【解决方案4】:

          查看绑定
          是否调用 DoubleToStringConverter?
          被调用了吗?

          Text="{Binding In1, Converter={StaticResource DoubleToStringConverter}, Source={StaticResource Converter}}"
          

          将 Source 从绑定中移到 DockableContent 上的 DataContext 中。

          DataContext="{Binding RelativeSource={RelativeSource self}}"
          

          不用转换器试试

          Text="{Binding In1}"
          

          【讨论】:

            猜你喜欢
            • 2014-12-13
            • 2012-01-15
            • 2020-06-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-27
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多