2015-12-21 22 views
1

我有一个命令,单击发送按钮时发送文本。绑定设置为双向,并且updatesource触发到property changed。但是文本框的值不会更改为包含在sendCommand中的string.empty,即使该命令能够为新消息获取更新的文本框值。文本框不从视图模型更新其值

public class BuddyChatViewModel : BaseViewModel 
{ 
    private string chat; 
    public string Chat 
    { 
     get { return chat; } 
     set 
     { 
      chat = value; 
      RaisePropertyChanged(); 
     } 
    }   

    public RelayCommand sendChatCommand { get; private set; } 
    string username = ""; 
    string buddy = ""; 
    UriStrings url = new UriStrings(); 
    BuddiesHomeModel buddiesList = new BuddiesHomeModel(); 
    HttpService http = new HttpService(); 
    StorageService store = new StorageService(); 
    string response = ""; 

    BuddyChatModel buddyChat = new BuddyChatModel(); 
    List<BuddyChat2Datum> buddychatList = new List<BuddyChat2Datum>(); 
    BuddyChat2Datum tempDatum = new BuddyChat2Datum(); 
    private ObservableCollection<BuddyChat2Datum> buddyChatOC = new ObservableCollection<BuddyChat2Datum>(); 
    public ObservableCollection<BuddyChat2Datum> BuddyChatOC 
    { 
     get { return buddyChatOC; } 
     set 
     { 
      buddyChatOC = value; 
      RaisePropertyChanged(); 
     } 
    } 


    private async void sendChatExecute() 
    {    
     int i = 0; 
     string s = url.buddychatText(username, buddy, chat); 
     chat = ""; 
     response = await http.GetAsync(s);    
     buddyChat = JsonConvert.DeserializeObject<BuddyChatModel>(response); 
     buddychatList.Clear(); 
     for (i = 0; i < buddyChat.data.Count; i++) 
     { 
      tempDatum.conversation = buddyChat.data[i].conversation; 
      tempDatum.datetime = buddyChat.data[i].datetime; 
      tempDatum.from = buddyChat.data[i].from; 
      tempDatum.to = buddyChat.data[i].to; 
      if (tempDatum.from == username) 
       tempDatum.isLeft = false; 
      else 
       tempDatum.isLeft = true; 
      buddychatList.Add(tempDatum); 
      tempDatum = new BuddyChat2Datum(); 
     } 
     BuddyChatOC.Clear(); 
     for (i = 0; i < buddychatList.Count; i++) 
     { 
      BuddyChatOC.Add(buddychatList[i]); 
     } 
     Navigate<BuddyChatViewModel>(buddychatList); 
    } 



    #region State Management 

    public override void LoadState(object navParameter, Dictionary<string, object> state) 
    { 
     sendChatCommand = new RelayCommand(sendChatExecute); 
     int i = 0; 
     base.LoadState(navParameter, state); 
     BuddyChatOC.Clear(); 
     // load test items again; in production this would retrieve the live item by id or get it from a local data cache 
     List<BuddyChat2Datum> buddychatList = (List<BuddyChat2Datum>)navParameter; 
     //var mes = new MessageDialog(buddychatList.Count.ToString()); 
     //await mes.ShowAsync(); 
     for(i=0;i<buddychatList.Count;i++) 
     { 
      BuddyChatOC.Add(buddychatList[i]); 
     } 
     username = buddychatList[i-1].username; 
     buddy = buddychatList[i-1].buddy;   
    } 


    public override void SaveState(Dictionary<string, object> state) 
    { 
     base.SaveState(state); 

    } 

    #endregion 
} 

}

XAML代码:

<Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="auto"/> 
     </Grid.RowDefinitions> 
     <ListView x:Name="chatList" HorizontalAlignment="Stretch" ItemsSource="{Binding BuddyChatOC}" ItemTemplateSelector="{StaticResource ChatSelector}"> 
      <ListView.ItemContainerStyle> 
       <Style TargetType="ListViewItem"> 
        <Setter Property="HorizontalAlignment" Value="Stretch" /> 
        <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
       </Style> 
      </ListView.ItemContainerStyle> 

     </ListView> 
     <RelativePanel Grid.Row="1" Margin="5,10,5,10"> 
      <TextBox x:Name="sendtext" Margin="0,0,2,0" Text="{Binding Chat, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" RelativePanel.AlignLeftWithPanel="True" RelativePanel.LeftOf="sendtextbutton"/> 
      <Button x:Name="sendtextbutton" Content="Send" Command="{Binding sendChatCommand}" RelativePanel.AlignRightWithPanel="True" >      
      </Button> 
     </RelativePanel> 
    </Grid> 
+0

必须执行'INotofyPropertyChanged' –

+0

在BuddyChatOC二传手,你应该有RaisePropertyChanged( “BuddyChatOC”); – Slasko

+0

问题是不是与buddychatoc .. listview已被正确更新..问题是与“聊天”字符串,这是文本框的绑定属性 – KartheekJ

回答

0

实现BuddyChatViewModel INotifyPropertyChanged的

public class BuddyChatViewModel : INotifyPropertyChanged, BaseViewModel 
{ 
private string chat; 
public string Chat 
{ 
    get { return chat; } 
    set 
    { 
     chat = value; 
     NotifyPropertyChanged("Chat"); 
    } 
} 


//INotifyPropertyChanged Members 

public event PropertyChangedEventHandler PropertyChanged; 
private void NotifyPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

} 
0

如果你使用MVVMLight(从这个问题是如何标记我认为你这样做),你需要在指定更改属性名RaisePropertyChanged调用。

这应该工作你的情况:

public string Chat 
    { 
     get { return chat; } 
     set 
     { 
      chat = value; 
      RaisePropertyChanged(() => Chat); 
     } 
    } 
+0

视图没有得到更新 – KartheekJ