文本框不更新

时间:2012-09-07 11:58:47

标签: c# wpf data-binding inotifypropertychanged

我试过搜索,但也许我没有使用正确的术语来搜索。

我有几个即时使用的文本框,当我输入数据时,我看到调试时更新的值,但它们永远不会更新到表单。

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

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

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");
        }
    }
}

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

您可以使用模式UpdateSourceTrigger=PropertyChanged

添加约束TwoWay
<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"
/> 

答案 2 :(得分:0)

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

public class AngleConverter : INotifyPropertyChanged

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

答案 3 :(得分:0)

审查约束力 DoubleToStringConverter会被调用吗? 被叫?

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

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

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

尝试不使用转换器

Text="{Binding In1}"
相关问题