170 likes | 303 Views
Silverlight 5 , XAML и связывание данных. Сергей Пугачёв Microsoft MVP @ spugachev. Сделали то , что просили…. XAML и связывание данных. ImplicitDataTemplates RelativeSource Ancestor Bindings Binding In Style Setters Markup Extensions Databinding Debugging
E N D
Silverlight 5, XAML и связывание данных Сергей ПугачёвMicrosoft MVP @spugachev
XAML и связывание данных • ImplicitDataTemplates • RelativeSource Ancestor Bindings • Binding In Style Setters • Markup Extensions • DatabindingDebugging • DataContextChangedEvent (Post Beta) • UpdateSourceTrigger(Post Beta)
Implicit Data Templates • Шаблоны основаны на типе • Учитывается иерархия наследования <ListBoxItemsSource="{BindingVehicles}"/> <Application.Resources> <ResourceDictionary> <!--Default Vehicle DataTemplate--> <DataTemplateDataType="models:Vehicle"> <Image Source="Vehicle.png"/> </DataTemplate> <DataTemplateDataType="models:Car"> <Image Source="Car.png"/> </DataTemplate> <DataTemplateDataType="models:Truck"> <Image Source="Truck.png"/> </DataTemplate> <DataTemplateDataType="models:Motorcycle"> <Image Source="Motorcycle.png"/> </DataTemplate> </ResourceDictionary> </Application.Resources>
Relative Source <DataTemplate x:Key="StateComboBoxDataTemplate"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{BindingStateCode}"Margin="0,0,5,0"/> <TextBlock Text="{BindingStateName}" Visibility="{BindingIsDropDownOpen, RelativeSource={RelativeSourceFindAncestor AncestorType=ComboBox}, Converter={StaticResourceBoolToVisibilityConverter}}"/> </StackPanel>
Markup Extensions publicclassMethodInvokeExtension: IMarkupExtension<object> { // Properties Exposed in XAML as Intellisense Love publicStringMethod { get; set; } // Invoked by the XAML Parser @ runtime publicobjectProvideValue(IServiceProviderserviceProvider) { <UserControl x:Class="AppointmentsView" xmlns:MyUtils="clr-namespace:TestApp;assembly=TestApp"> ... <StackPanel x:Name="LayoutRoot"> <ComboBox Name="appointmentsListBox" SelectionChanged="{MyUtils:MethodInvoke Method=OnAppointmentChanged}"
Binding Style Setters • Создайте класс для представления значений • Создайте экземпляр класса в ресурсах • Используйте связывание данных в стилях namespaceMyProject.Styles { publicclassMyAppStyles: INotifyPropertyChanged{ publicBrushForegroundColor{ get{ return _foregroundColor; } set { _foregroundColor = value; NotifyPropertyChanged("ForegroundColor"); <ResourceDictionaryxmlns:stylesNS="clr-namespace:MyProject.Styles"> <stylesNS:MyAppStyles x:Key="MyAppStyles"/> <StyleTargetType="TextBlock"> <Setter Property="Foreground" Value="{BindingForegroundColor, Source={StaticResourceMyAppStyles}}"
Coming Soon post beta DataContextChanged this.DataContextChanged += View_DataContextChanged; … voidView_DataContextChanged(objectsender, DependencyPropertyChangedEventArgs e) { INotifyPropertyChanged customer; customer = e.OldValueasINotifyPropertyChanged; if (customer != null) customer.PropertyChanged -= customer_PropertyChanged; customer = e.NewValueasINotifyPropertyChanged; if (customer != null) customer.PropertyChanged += customer_PropertyChanged; }
Coming Soon post beta UpdateSourceTrigger <TextBox Name="vinTextBox" Text="{BindingSelectedClaim.InsuredVIN, Mode=TwoWay}" UpdateSourceTrigger=PropertyChanged}"
XAML и связывание данных • ImplicitDataTemplates • RelativeSource Ancestor Bindings • Binding In Style Setters • Markup Extensions • DatabindingDebugging • DataContextChangedEvent (Post Beta) • UpdateSourceTrigger(Post Beta)
Silverlight 5, XAML и DataBinding Сергей ПугачёвMicrosoft MVP @spugachev