Here is a control that does this:
using System.Windows; using System.Windows.Controls; using System.Windows.Data; public class KeyValueControl : Control { public static readonly DependencyProperty KeyProperty = DependencyProperty.Register( "Key", typeof(string), typeof(KeyValueControl), new PropertyMetadata(default(string))); public static readonly DependencyProperty ValueProperty = DependencyProperty.Register( "Value", typeof(object), typeof(KeyValueControl), new FrameworkPropertyMetadata { DefaultValue = null, BindsTwoWayByDefault = true, DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); static KeyValueControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(KeyValueControl), new FrameworkPropertyMetadata(typeof(KeyValueControl))); } public string Key { get { return (string)GetValue(KeyProperty); } set { SetValue(KeyProperty, value); } } public object Value { get { return GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } }
Style:
<Style TargetType="{x:Type local:KeyValueControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:KeyValueControl}"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="100"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Key, RelativeSource={RelativeSource TemplatedParent}}"/> <TextBox Grid.Column="1" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
Usage (creating a property grid):
<ItemsControl> <customControls:KeyValueControl Key="First" Value="{Binding Value1}" /> <customControls:KeyValueControl Key="Second" Value="{Binding Value2}" /> <customControls:KeyValueControl Key="Last" Value="{Binding Value3}" /> <customControls:KeyValueControl Key="Bool1" Value="{Binding Bool1}" Style="{StaticResource CheckBoxStyle}"/> <customControls:KeyValueControl Key="Bool2" Value="{Binding Bool2}" Style="{StaticResource CheckBoxStyle}"/> </ItemsControl>
source share