2014-05-09 52 views
0

在我的程序中,我有一個TextBlock綁定到數據模型中的屬性值。我想將此TextBlock更改爲TextBox,以便它是可編輯的,但仍希望將文本綁定到它。問題是,當我將TextBlock更改爲TextBox時,我的綁定不起作用,並且TextBox顯示爲空白。無法執行與文本框等效的文本綁定

我綁定的屬性是我在程序中定製的類。

這是屬性:

//Property for Display Name 
public MultiItemString DisplayName {} 

這是MultiItemString類:

public class MultiItemString : INotifyPropertyChanged 
{ 
    private readonly string[] _keys; 
    private readonly MultiItemString[] _nestedItems; 

    readonly bool _resourceKey; 
    private readonly bool _nestedMultiItemStrings; 

    public MultiItemString(IEnumerable<string> keys, bool resourceKey = true) 
    { 
     _keys = keys.ToArray(); 
     _resourceKey = resourceKey; 

     LanguageChange.LanguageChagned += (sender, args) => RaisePropertyChanged(""); 
    } 

    public MultiItemString(IEnumerable<MultiItemString> nestedItems) 
    { 
     _nestedItems = nestedItems.ToArray(); 
     foreach (var multiItemString in _nestedItems) 
     { 
      multiItemString.PropertyChanged += (s, e) => RaisePropertyChanged("Value"); 
     } 
     _nestedMultiItemStrings = true; 
    } 

    public string Key 
    { 
     get 
     { 
      if (_keys != null && _keys.Length != 0) return _keys[0]; 
      return null; 
     } 
    } 

    public string Value 
    { 
     get 
     { 
      var sb = new StringBuilder(); 
      if (_nestedMultiItemStrings) 
      { 
       foreach (var MultiItemString in _nestedItems) 
       { 
        sb.Append(MultiItemString.Value); 
       } 
      } 
      else 
      { 
       foreach (var key in _keys) 
       { 
        sb.Append(_resourceKey ? (string)Application.Current.Resources[key] : key); 
       } 
      } 
      return sb.ToString(); 
     } 
    } 

    public override string ToString() 
    { 
     return Value; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    void RaisePropertyChanged(string propertyName) 
    { 
     var temp = PropertyChanged; 
     if (temp != null) 
      temp(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

我的XAML:

<TextBox Text="{Binding Model.DisplayName}" Height="28" HorizontalAlignment="Left" Name="title_TB" VerticalAlignment="Top" Width="Auto" FontWeight="Bold" FontSize="14" Margin="5,2,0,0" /> 

我怎麼能這個屬性綁定到一個TextBox作爲它是文本值,就像我對所做的一樣?

回答

2

這應該工作:

<TextBox Text="{Binding Model.DisplayName.Value}" Height="28" HorizontalAlignment="Left" Name="title_TB" VerticalAlignment="Top" Width="Auto" FontWeight="Bold" FontSize="14" Margin="5,2,0,0" /> 

而且,更新Value屬性具有setter方法:

private string _thisShouldBeAValidField ; 
    public string Value 
     { 
      get 
      { 
if(_thisShouldBeAValidField!=null) return _thisShouldBeAValidField; 
       var sb = new StringBuilder(); 
       if (_nestedMultiItemStrings) 
       { 
        foreach (var MultiItemString in _nestedItems) 
        { 
         sb.Append(MultiItemString.Value); 
        } 
       } 
       else 
       { 
        foreach (var key in _keys) 
        { 
         sb.Append(_resourceKey ? (string)Application.Current.Resources[key] : key); 
        } 
       } 
       return sb.ToString(); 
      } 
    set{ 
    _thisShouldBeAValidField = value; 
    } 
     } 
+0

它不工作。我想你只是把'TextBlock'改成了'TextBox',不是嗎? –

+0

再次閱讀問題,並更新了答案。希望有所幫助。 –

+0

好吧,至少在我現在添加'.Value'時不會崩潰,但是什麼是_thisShouldBeAValidField? –