xxxxxxxxxx
public static string GetRowHeights(DependencyObject obj)
{
return (string)obj.GetValue(RowHeightsProperty);
}
public static void SetRowHeights(DependencyObject obj, string value)
{
obj.SetValue(RowHeightsProperty, value);
}
public static readonly DependencyProperty
RowHeightsProperty = DependencyProperty.RegisterAttached("RowHeights",
typeof(string), typeof(Ex),
new PropertyMetadata("", RowHeightsChanged));
xxxxxxxxxx
private static void SelectOnEntryChanged(DependencyObject d,
DependencyPropertyChangedEventArgs args)
{
if (!(bool) args.NewValue) return;
var text = d as TextBox;
if (text == null) return;
text.GotFocus += (s, e) =>
{
text.SelectionStart = 0;
text.SelectionLength = text.Text.Length;
};
}
xxxxxxxxxx
<Style TargetType="TextBox">
<Setter Property="my:Ex.SelectOnEntry" Value="True"/>
</Style>
xxxxxxxxxx
<Style TargetType="Grid">
<Setter Property="my:Ex.RowHeights" Value="*,Auto,25"/>
</Style>
xxxxxxxxxx
public static string GetRowHeights(DependencyObject obj)
{
return (string)obj.GetValue(RowHeightsProperty);
}
public static void SetRowHeights(DependencyObject obj, string value)
{
obj.SetValue(RowHeightsProperty, value);
}
// Using a DependencyProperty as the backing store for RowHeights.
// This enables animation, styling, binding, etc...
public static readonly DependencyProperty RowHeightsProperty =
DependencyProperty.RegisterAttached("RowHeights",
typeof(string), typeof(Ex),
new PropertyMetadata("", RowHeightsChanged));
private static void RowHeightsChanged(DependencyObject d,
DependencyPropertyChangedEventArgs args)
{
var grid = d as Grid;
if (grid == null) return;
grid.RowDefinitions.Clear();
var definitions = args.NewValue.ToString();
if (string.IsNullOrEmpty(definitions)) return;
var heights = definitions.Split(',');
foreach (var height in heights)
{
if (height == "Auto")
grid.RowDefinitions.Add(
new RowDefinition{Height=GridLength.Auto});
else if (height.EndsWith("*"))
{
var height2 = height.Replace("*", "");
if (string.IsNullOrEmpty(height2)) height2 = "1";
var numHeight = int.Parse(height2);
grid.RowDefinitions.Add(new RowDefinition
{
Height = new GridLength(numHeight, GridUnitType.Star)
});
}
else
{
var numHeight = int.Parse(height);
grid.RowDefinitions.Add(new RowDefinition
{
Height = new GridLength(numHeight, GridUnitType.Pixel)
});
}
}
}
xxxxxxxxxx
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
xxxxxxxxxx
public class Ex : DependencyObject
{
public static readonly DependencyProperty
SecurityIdProperty = DependencyProperty.RegisterAttached(
"SecurityId", typeof(string), typeof(Ex), new PropertyMetadata(""));
public static string GetSecurityId(DependencyObject d)
{
return (string) d.GetValue(SecurityIdProperty);
}
public static void SetSecurityId(DependencyObject d, string value)
{
d.SetValue(SecurityIdProperty, value);
}
}
xxxxxxxxxx
public class TextBoxEx : TextBox
{
public string SecurityId
{
get
{
return (string)GetValue(SecurityIdProperty);
}
set { SetValue(SecurityIdProperty, value); }
}
public static readonly DependencyProperty
SecurityIdProperty = DependencyProperty.Register("SecurityId",
typeof(string), typeof(TextBoxEx), new PropertyMetadata(""));
}