Warszawa-Session 2
Download
Report
Transcript Warszawa-Session 2
WinRT Apps
23.10.2014.
http://msdn.microsoft.com/en-us/library/hh821028.aspx
<Page
…
…
DataContext="{Binding Source={StaticResource Locator}, Path=Main}" />
<TextBlock x:Name="DirectionsTextBlock" TextWrapping="Wrap"
Margin="12,0,0,0" Text="{Binding Directions}" />
<TextBlock x:Name="DirectionsTextBlock" TextWrapping="Wrap"
Margin="12,0,0,0" Text="{Binding Directions, Mode=OneWay}" />
public class ItemViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
…
}
Subscribes to
public class ItemViewModel : INotifyPropertyChanged
{
private string _id;
/// Sample ViewModel property;
public string ID
{
get { return _id; }
set {
if (value != _id) {
_id = value;
NotifyPropertyChanged("ID"); }
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ItemViewModel : INotifyPropertyChanged {
// Properties
private string _id;
public string ID
{
get { return _id; }
set { this.SetProperty(ref this._id, value); }
}
// Property Change logic
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
if (object.Equals(storage, value)) return false;
storage = value;
this.OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = this.PropertyChanged;
if (eventHandler != null)
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
<ListView x:Name="IngredientsLIstBox"
ItemTemplate="{StaticResource MyDataTemplate}"
DataContext="{StaticResource RecipeViewModel}"/>
ItemsSource="{Binding Ingredients}"/>
public class RecipeDetails : INotifyPropertyChanged
{
/// <summary>
/// A collection for ItemViewModel objects.
/// </summary>
public ObservableCollection<ItemViewModel> Items { get; private set; }
public void LoadData()
{
this.Items.Add(new ItemViewModel() { ID = "0", LineOne = "runtime one", LineTwo = ... });
this.Items.Add(new ItemViewModel() { ID = "1", LineOne = "runtime two", LineTwo = ... });
this.Items.Add(new ItemViewModel() { ID = "2", LineOne = "runtime three", LineTwo =...});
}
...
}
<Button Content="{Binding Path=NextItem, Mode=OneWay,
TargetNullValue={Binding Path=NullValue}}" />
<TextBlock Text="{Binding Path=badPath,
FallbackValue='this is a fallback value'}"
Grid.Column="1"> </TextBlock>