2012-09-07 56 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}"