2012-09-07 57 views
0

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

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

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

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

I.E.

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

     OnPropertyChanged("In1"); 
    } 
} 

无论什么,我的问题是,我没有看到值写入文本框。这是使用数据绑定这样的IM假设IM做一些不正确的(明确)

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

回答

1

尝试在你的文本框应用UpdateSourceTrigger=PropertyChanged的我第一次真正endevour:

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

你真正的代码应该有一些像这样:

public class AngleConverter : INotifyPropertyChanged 

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

1

您可以添加到您与您的模式结合UpdateSourceTrigger=PropertyChangedTwoWay

<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" 
/> 
0

审查结合
是否DoubleToStringConverter被调用?
被调用?

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

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

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

尽量不带有转换

Text="{Binding In1}"