I am currently implementing a search (more properly "filter") form for my View that uses ApplicationBarIconButtons to drive user interactions with the form: a search button to transition to the VisualState with the form displayed, and one to clear the current filter string value. The textbox that accepts the filter is bound to a property on the ViewModel:
XAML
<toolkit:PhoneTextBox x:Name="txtSearch" Text="{Binding VisitsFilter, Mode=OneWay}" />
ViewModel
private string _visitsFilter;
public string VisitsFilter
{
get
{
return _visitsFilter;
}
set
{
_visitsFilter = value;
RaisePropertyChanged("VisitsFilter");
RebuildVisits();
}
}
The problem is that ApplicationBarIconButtons don't really have any ability to bind to points on my ViewModel with Commands or similar, so I handle their interactions with it in a code behind handler for its Click event. Doesn't seem like it should be that big a deal... Get the ViewModel from the page's data context and set the value of the bound property:
Code Behind
private VisitsViewModel ViewModel
{
get
{
return this.DataContext as VisitsViewModel;
}
}
private void abbClear_Click(object sender, EventArgs e)
{
this.Focus();
ViewModel.VisitsFilter = string.Empty;
}
If you follow the code above through the setter, you'll see that I set the value of the private string member, then raise the event that the property has changed. I actually have a code behind subscription to this event in my View that's performing other logic about making the "Clear" button visible or not, but the point is it is listening successfully on that event. However, the OneWay binding in the markup above doesn't update the value of the Text property on my PhoneTextBox.
Where's the disconnect here?