Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <UserControl.Resources>
- <src:Person x:Key="personSource" />
- <src:Job x:Key="jobSource" />
- </UserControl.Resources>
- <TextBox Text="{Binding Source={StaticResource personSource}, Path=Name" />
- <TextBox Text="{Binding Source={StaticResource jobSource}, Path=Address" />
- Person personSource = FindResource("personSource") as Person;
- personSource = externalPerson;
- Job jobSource= FindResource("jobSource") as Job;
- jobSource = externalJob;
- public class CustomControl : Control
- {
- public static readonly DependencyProperty PersonProperty =
- DependencyProperty.Register("Person", typeof(Person), typeof(CustomControl), new UIPropertyMetadata(null));
- public Person Person
- {
- get { return (Person) GetValue(PersonProperty); }
- set { SetValue(PersonProperty, value); }
- }
- public static readonly DependencyProperty JobProperty =
- DependencyProperty.Register("Job", typeof(Job), typeof(CustomControl), new UIPropertyMetadata(null));
- public Job Job
- {
- get { return (Job) GetValue(JobProperty); }
- set { SetValue(JobProperty, value); }
- }
- static CustomControl()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
- }
- }
- <ResourceDictionary
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="clr-namespace:WpfApplication3">
- <Style TargetType="{x:Type local:CustomControl}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type local:CustomControl}">
- <Border Background="{TemplateBinding Background}"
- BorderBrush="{TemplateBinding BorderBrush}"
- BorderThickness="{TemplateBinding BorderThickness}">
- <StackPanel>
- <TextBox Text="{TemplateBinding Person.Name}" />
- <TextBox Text="{TemplateBinding Job.Address}" />
- </StackPanel>
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </ResourceDictionary>
- <src:CustomControl Person="{Binding Person}" Job="{Binding Job}" />
- DataContext="{Binding RelativeSource={RelativeSource Self}}"
- <TextBox Text="{Binding Path=Person.Name" />
- <TextBox Text="{Binding Path=Job.Address" />
- <TextBox Text="{Binding Source=Person, Path=Name" />
- <TextBox Text="{Binding Source=Job, Path=Address" />
Add Comment
Please, Sign In to add comment