WP7备注(33)(DataContext)

DataContext为所有内部元素提供所需要的数据源

实例1:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel DataContext="{Binding Source={StaticResource clock}}"
Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="{Binding Path=Hour}" />
<TextBlock Text="{Binding Path=Minute,
Converter={StaticResource stringFormat},
ConverterParameter=':{0:D2}'}" />
<TextBlock Text="{Binding Path=Second,
Converter={StaticResource stringFormat},
ConverterParameter=':{0:D2}'}" />
</StackPanel>
</Grid>

实例2:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel DataContext="{Binding Source={StaticResource clock}}"
Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="{Binding Hour}" />
<TextBlock Text="{Binding Minute,
Converter={StaticResource stringFormat},
ConverterParameter=':{0:D2}'}" />
<TextBlock Text="{Binding Second,
Converter={StaticResource stringFormat},
ConverterParameter=':{0:D2}'}" />
</StackPanel>
</Grid>

实例3:

<Border DataContext="{Binding ElementName=this}"
Background="{Binding Background}"
BorderBrush="{Binding BorderBrush}"
BorderThickness="{Binding BorderThickness}"
CornerRadius="{Binding CornerRadius}"
Padding="{Binding Padding}">
<TextBlock Text="{Binding Path=Text}"
TextAlignment="{Binding Path=TextAlignment}"
TextDecorations="{Binding Path=TextDecorations}"
TextWrapping="{Binding ElementName=this, Path=TextWrapping}" />
</Border>

实例4:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel DataContext="{StaticResource clock}"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="It's day number " />
<TextBlock Text="{Binding Date.Day}" />
<TextBlock Text=" of month " />
<TextBlock Text="{Binding Date.Month}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text=" of the year " />
<TextBlock Text="{Binding Date.Year}" />
<TextBlock Text=", a " />
<TextBlock Text="{Binding Date.DayOfWeek}" />
<TextBlock Text="." />
</StackPanel>
</StackPanel>
</Grid>

实例5:

public class TwelveHourClock : Clock
{
int hour12;
bool isam, ispm;
public int Hour12
{
protected set
{
if (value != hour12)
{
hour12 = value;
OnPropertyChanged(new PropertyChangedEventArgs("Hour12"));
}
}
get
{
return hour12;
}
}
public bool IsAm
{
protected set
{
if (value != isam)
{
isam = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsAm"));
}
}
get
{
return isam;
}
}
public bool IsPm
{
protected set
{
if (value != ispm)
{
ispm = value;
OnPropertyChanged(new PropertyChangedEventArgs("IsPm"));
}
}
get
{
return ispm;
}
}
protected override void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (args.PropertyName == "Hour")
{
Hour12 = (Hour - 1) % 12 + 1;
IsAm = Hour < 12;
IsPm = !IsAm;
}
base.OnPropertyChanged(args);
}
}
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return (Visibility)value == Visibility.Visible;
}
}
<phone:PhoneApplicationPage.Resources>
<petzold:TwelveHourClock x:Key="clock12" />
<petzold:BooleanToVisibilityConverter x:Key="booleanToVisibility" />
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel DataContext="{StaticResource clock12}"
Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="It's after " />
<TextBlock Text="{Binding Hour}" />
<TextBlock Text=" in the morning."
Visibility="{Binding IsAm,
Converter={StaticResource booleanToVisibility}}"
/>
<TextBlock Text=" in the afternoon."
Visibility="{Binding IsPm,
Converter={StaticResource
booleanToVisibility}}"/>
</StackPanel>
</Grid>

实例6:

public class Adder : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
double augend = 0;
double addend = 0;
double sum = 0;
public double Augend
{
set
{
if (augend != value)
{
augend = value;
OnPropertyChanged(new PropertyChangedEventArgs("Augend"));
CalculateNewSum();
}
}
get
{
return augend;
}
}
public double Addend
{
set
{
if (addend != value)
{
addend = value;
OnPropertyChanged(new PropertyChangedEventArgs("Addend"));
CalculateNewSum();
}
}
get
{
return addend;
}
}
public double Sum
{
protected set
{
if (sum != value)
{
sum = value;
OnPropertyChanged(new PropertyChangedEventArgs("Sum"));
}
}
get
{
return sum;
}
}
void CalculateNewSum()
{
Sum = Augend + Addend;
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (PropertyChanged != null)
PropertyChanged(this, args);
}
}
<phone:PhoneApplicationPage.Resources>
<petzold:Adder x:Key="adder" />
<petzold:StringFormatConverter x:Key="stringFormat" />
</phone:PhoneApplicationPage.Resources>

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
DataContext="{Binding Source={StaticResource adder}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Slider Grid.Row="0"
Minimum="-100"
Maximum="100"
Margin="24"
Value="{Binding Augend, Mode=TwoWay}" />
<Slider Grid.Row="2"
Minimum="-100"
Maximum="100"
Margin="24"
Value="{Binding Addend, Mode=TwoWay}" />
<TextBlock Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="48"
Text="{Binding Sum,
Converter={StaticResource stringFormat},
ConverterParameter=' {0:F2} '}" />
</Grid>
原文地址:https://www.cnblogs.com/otomii/p/2035292.html