![](/img/trans.png)
[英]how to bind the model to view from viewmodel in windows phone app
[英]How to bind the ViewModel for a windows phone 8 messaging app
My model:
public class MyMessageModel
{
public string DisplaySender { get; set; }
//how does the below observable collection needs to be changed ,
//if I want to add another field to itemssource template.
//e.g. public DateTime Timestamp { get; set; }
public ObservableCollection<string> MessagesExchanged { get; set; }
public string NewMessage { get; set; }
}
Chat.xaml:
<TextBlock Name="lblFromUserName" Text="{Binding DisplaySender ,Mode=TwoWay}" Height="65" Style="{StaticResource PhoneTextNormalStyle}" FontSize="35"/>
<ItemsControl ItemsSource="{Binding Path=MessagesExchanged}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ????,Mode=TwoWay}" />
<TextBlock Text="{Binding Path=Timestamp}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Grid.Row="1"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Column="0" Name="txtNewMessage" Text="{Binding NewMessage,Mode=TwoWay}" Margin="0,0,0,0" Width="350"/>
<Button Grid.Column="1" Command="{Binding SendClickCommand,Mode=TwoWay}" Name="btnSend" Content="Send" Width="100" />
</StackPanel>
Chat.xaml.cs如下所示:
public class Chat: PhoneApplicationPage
{
private MyMessageViewModel _MyMessageViewModel;
public Conversation()
{
InitializeComponent();
_MyMessageViewModel = new MyMessageViewModel();
this.DataContext = _MyMessageViewModel;
}
}
我的ViewModel MyMessageViewModel如下所示:
public System.Windows.Input.ICommand SendClickCommand
{
get
{
return new DelegateCommand((o) =>
{
Task.Factory.StartNew(() =>
{
//loop through the selected items and clear everything
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
try
{
//DO YOUR WORK HERE: TAKE THE NEW MESSAGE AND APPEND IT TO THE MESSAGES EXCHANGED
}
catch (Exception)
{
throw;
}
});
});
});
}
}
现在,当用户在上面称为Chat.xaml的视图中时(用户将从主页访问此页面),我想在其顶部加载DisplaySender值,该值将在整个对话过程中固定。作为主页的导航参数。
而且,每次用户单击“发送”按钮时,SendClickCommand中的添加仅通过从txtNewMessage字段添加新消息来更新MessagesExchanged集合,然后再清除此字段。
我在这里有两个问题:
当用户首次访问Chat.xaml时,如何绑定三个字段的数据,例如DisplaySender(非空值将作为导航参数传递),MessagesExchanged(最初启动新对话时为空,否则它将具有导航参数中的一个非空值)和NewMessage(最初,该值始终为空)。
其次,在SendClickCommand通知的属性中,我如何从txtNewMessage中获取文本并更新ObservableCollection MessagesExchanged,最后清除txtNewMessage的值。以及如何将MessagesExchanged的值绑定到datatemplate textblock字段?
我猜您正在尝试从HomePage导航到ChatPage时传递MyMessageModel类的对象。
所以定义一个属性
private MyMessageModel currentMessageModel;
public MyMessageModel CurrentMessageModel
{
get { return currentMessageModel; }
set { currentMessageModel = value; }
}
并在ChatPage的OnNavigatedTo方法中设置
CurrentMessageModel=PassedObjectOfMessageModel
XAML:
<TextBlock Name="lblFromUserName" Text="{Binding CurrentMessageModel.DisplaySender ,Mode=TwoWay}" Height="65" Style="{StaticResource PhoneTextNormalStyle}" FontSize="35"/>
<ItemsControl ItemsSource="{Binding Path=CurrentMessageModel.MessagesExchanged}">
//No need for data template as collection only contains string
<ItemsControl
<StackPanel Orientation="Horizontal" Grid.Row="1">
<TextBox Grid.Column="0" Name="txtNewMessage" Text="{Binding NewMessage,Mode=TwoWay}" Margin="0,0,0,0" Width="350"/>
<Button Grid.Column="1" Command="{Binding SendClickCommand,Mode=TwoWay}" Name="btnSend" Content="Send" Width="100" />
</StackPanel>
//C#
CurrentMessageModel.MessagesExchanged.Add(txtNewMessage.Text);
并且您不需要任何文本块来显示ObservableCollection,因为您的集合仅包含字符串,因此只需将ItemsSource设置为集合即可显示数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.